我读过关于使用比较器排序数组列表的内容,但在所有的例子中,人们都使用了compareTo,根据一些研究,它是字符串的一种方法。

我想根据自定义对象的一个属性(Date对象)对其数组列表进行排序 (getStartDay())。通常我通过item1.getStartDate().before(item2.getStartDate())比较它们,所以我想知道我是否可以写一些像这样的东西:

public class CustomComparator {
    public boolean compare(Object object1, Object object2) {
        return object1.getStartDate().before(object2.getStartDate());
    }
}

public class RandomName {
    ...
    Collections.sort(Database.arrayList, new CustomComparator);
    ...
}

当前回答

我已经尝试了很多不同的解决方案,可在互联网上,但解决方案,为我是可在下面的链接。

https://www.java67.com/2017/07/how-to-sort-arraylist-of-objects-using.html

其他回答

1.8以来的新功能是一个List.sort()方法,而不是使用Collection.sort() 直接调用mylistcontainer。sort()

下面是一个演示List.sort()特性的代码片段:

List<Fruit> fruits = new ArrayList<Fruit>();
fruits.add(new Fruit("Kiwi","green",40));
fruits.add(new Fruit("Banana","yellow",100));
fruits.add(new Fruit("Apple","mixed green,red",120));
fruits.add(new Fruit("Cherry","red",10));

// a) using an existing compareto() method
fruits.sort((Fruit f1,Fruit f2) -> f1.getFruitName().compareTo(f2.getFruitName()));
System.out.println("Using String.compareTo(): " + fruits);
//Using String.compareTo(): [Apple is: mixed green,red, Banana is: yellow, Cherry is: red, Kiwi is: green]

// b) Using a comparable class
fruits.sort((Fruit f1,Fruit f2) -> f1.compareTo(f2));  
System.out.println("Using a Comparable Fruit class (sort by color): " + fruits);
// Using a Comparable Fruit class (sort by color): [Kiwi is green, Apple is: mixed green,red, Cherry is: red, Banana is: yellow]

Fruit类是:

public class Fruit implements Comparable<Fruit>
{
    private String name;
    private String color;
    private int quantity;

    public Fruit(String name,String color,int quantity)
    { this.name = name; this.color = color; this.quantity = quantity; }

    public String getFruitName() { return name; }        
    public String getColor() { return color; }  
    public int getQuantity() { return quantity; }

    @Override public final int compareTo(Fruit f) // sorting the color
    {
        return this.color.compareTo(f.color);
    }     
    @Override public String toString()
    {   
        return (name + " is: " + color);
    }
} // end of Fruit class   

是的,你可以。比较项有两个选项,Comparable接口和Comparator接口。

这两个接口都允许不同的行为。Comparable允许您使对象的行为与您刚刚描述的String类似(实际上,String实现了Comparable)。第二个,Comparator,允许你做你要求做的事情。你会这样做:

Collections.sort(myArrayList, new MyComparator());

这将导致收集。方法来使用比较器的排序机制。如果数组列表中的对象实现了可比性,你可以这样做:

Collections.sort(myArrayList);

Collections类包含许多这些有用的常用工具。

在Java 8中,你可以为你的比较器使用方法引用:

import static java.util.Comparator.comparing;

Collections.sort(list, comparing(MyObject::getStartDate));

java8 lambda表达式

Collections.sort(studList, (Student s1, Student s2) ->{
        return s1.getFirstName().compareToIgnoreCase(s2.getFirstName());
});

OR

Comparator<Student> c = (s1, s2) -> s1.firstName.compareTo(s2.firstName);
studList.sort(c)

您可以在2016年德国斯图加特的Java论坛上看一看这个演讲。

只有少数幻灯片使用德语,99%的内容是“基于英语”的Java源代码;就像

someCollection.sort(
  OurCustomComparator
    .comparing(Person::getName)
    .thenComparing(Person::getId)
);

其中OurCustomComparator使用默认方法(和其他有趣的想法)。如图所示,导致非常简洁的代码,以选择一些getter方法进行排序;以及超级简单的排序标准链接(或反转)。

如果你对java8感兴趣,你可以在那里找到很多材料来开始学习。