public class Publication {
private String name;
private String author;
private int price;
private String language;}
public class Book extends Publication {
private int ISBN;

public Book(String name, String author, String language, int price, int ISBN) {
    super(name, author, language, price);
    this.ISBN = ISBN;

}

.

public class Book extends Publication {
private int ISBN;

public Book(String name, String author, String language, int price, int ISBN) {
    super(name, author, language, price);
    this.ISBN = ISBN;

}

考虑这些类

在另一节课上,我做了一个Book级的arraylist

now I want to sort and print this arraylist based on name and then author
but I don't know what can I do

推荐答案

List<Book> books = new ArrayList<>();

        //put elements in your list

        books.sort((b1,b2) ->{
            int comparator;
            if (b1.name==b2.name){
                comparator = b1.author.compareTo(b2.author);
            } else {
                comparator = b1.name.compareTo(b2.name);
            }
            return comparator;
        });

在列表中.短(100);b1是当前"书"的方式,b2是列表中的下一个"书",返回值定义b1是在b2之前还是之后;

b1和b2是Book类型的变量,您可以更改它们的名称.

注意:字符串.compareTo()方法;按字母顺序排序,但优先考虑大写字母,例如:它将在小写字母"a"之前排序大写字母"Z".

Java相关问答推荐

无法找到符号错误—Java—封装

转换为Biggram

Android Studio—java—在onBindViewHolder中,将断点和空白添加到BackclerView中

路径映射未发生

如何以干净的方式访问深度嵌套的对象S属性?

名称冲突具有相同的擦除

在JDK 1.8源代码中,为什么使用A-B 0来确定哪个更大,而不是A B?

为什么我的回收视图会显示重复的列表?

如何集成语义发布和BitBucket(Java项目)

为什么当我创建Robot对象时,JavaFX引发IlLegalStateException异常?

来自外部模块的方面(对于Java+Gradle项目)不起作用

将ByteBuffer异步写入InputStream或Channel或类似对象

Spring动态反序列化JSON可以是列表,也可以只是一个对象

如何从命令行编译包中的所有类?

如何在MPAndroidChart中的条形图上正确添加标签

读取ConcurrentHashMap中的可变对象

如何在 WebSphere Application Server 内的托管线程上运行 BatchEE 作业(job)?

在数组列表中找到对象后,未从数组中删除对象

我真的能很好地解释同步计数器和 AtomicInteger 的 JMH 结果吗

如何在Java中调用对象上预定义的接口方法列表?