我读过关于使用比较器排序数组列表的内容,但在所有的例子中,人们都使用了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);
    ...
}

当前回答

Java 8 Lambda缩短了排序。

Collections.sort(stdList, (o1, o2) -> o1.getName().compareTo(o2.getName()));

其他回答

您的自定义类可以实现“Comparable”接口,这需要CompareTo方法的实现。在CompareTo方法中,您可以定义一个对象小于或大于另一个对象意味着什么。所以在你的例子中,它看起来是这样的:

public class MyCustomClass implements Comparable<MyCustomClass>{

..........

 @Override
public int compareTo(MyCustomClass a) {
    if(this.getStartDate().before(a.getStartDate())){
        return -1;
    }else if(a.getStartDate().before(this.getStartDate())){
        return 1;
    }else {
        return 0;
    }
}

负数表示它小于与之比较的对象。正数表示这比对象的比较大,零表示对象相等。

然后,可以使用collections.sort(myList)对列表进行排序,而不必输入比较器。如果使用TreeSet或TreeMap等已排序的集合数据结构,则此方法还具有自动排序的优点。

如果您想阅读更多关于Comparable接口的信息,可以查看这篇文章(披露:我是作者;)) https://nullbeans.com/the-java-comparable-interface-automatic-sort-of-collections/

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   

您可以使用Bean Comparator对自定义类中的任何属性进行排序。

Java 8 Lambda缩短了排序。

Collections.sort(stdList, (o1, o2) -> o1.getName().compareTo(o2.getName()));

我更喜欢这个过程:

public class SortUtil
{    
    public static <T> List<T> sort(List<T> list, String sortByProperty)
    {
            Collections.sort(list, new BeanComparator(sortByProperty));
            return list;
    }
}

List<T> sortedList = SortUtil<T>.sort(unsortedList, "startDate");

如果你的对象列表有一个名为startDate的属性,你可以反复使用这个属性。你甚至可以链接它们startDate.time。

这要求你的对象是Comparable的,这意味着你需要一个compareTo, equals和hashCode实现。

是的,可以更快……但是现在你不必为每一种类型的排序创建一个新的Comparator。如果您可以节省开发时间并放弃运行时,您可能会选择这个。