Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

java - 按属性对自定义对象的ArrayList进行排序(Sort ArrayList of custom Objects by property)

I read about sorting ArrayLists using a Comparator but in all of the examples people used compareTo which according to some research is a method for Strings.

(我读过有关使用Comparator对ArrayList进行排序的信息,但在所有示例中,人们都使用了compareTo ,根据一些研究,它是String的一种方法。)

I wanted to sort an ArrayList of custom objects by one of their properties: a Date object ( getStartDay() ).

(我想按自定义对象的属性之一对ArrayList进行排序:Date对象( getStartDay() )。)

Normally I compare them by item1.getStartDate().before(item2.getStartDate()) so I was wondering whether I could write something like:

(通常,我通过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);
    ...
}
  ask by Samuel translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Since Date implements Comparable , it has a compareTo method just like String does.

(由于Date实现Comparable ,因此它具有String一样的compareTo方法。)

So your custom Comparator could look like this:

(因此,您的自定义Comparator可能如下所示:)

public class CustomComparator implements Comparator<MyObject> {
    @Override
    public int compare(MyObject o1, MyObject o2) {
        return o1.getStartDate().compareTo(o2.getStartDate());
    }
}

The compare() method must return an int , so you couldn't directly return a boolean like you were planning to anyway.

(compare()方法必须返回一个int ,因此无论如何您都不能直接返回一个boolean 。)

Your sorting code would be just about like you wrote:

(您的排序代码几乎就像您写的那样:)

Collections.sort(Database.arrayList, new CustomComparator());

A slightly shorter way to write all this, if you don't need to reuse your comparator, is to write it as an inline anonymous class:

(如果您不需要重用比较器,则编写所有这些内容的一种更短的方法是将其编写为内联匿名类:)

Collections.sort(Database.arrayList, new Comparator<MyObject>() {
    @Override
    public int compare(MyObject o1, MyObject o2) {
        return o1.getStartDate().compareTo(o2.getStartDate());
    }
});

Since (自从)

You can now write the last example in a shorter form by using a lambda expression for the Comparator :

(现在,您可以通过对Comparator使用lambda表达式 ,以较短的形式编写最后一个示例:)

Collections.sort(Database.arrayList, 
                        (o1, o2) -> o1.getStartDate().compareTo(o2.getStartDate()));

And List has a sort(Comparator) method, so you can shorten this even further:

(而且List具有sort(Comparator)方法,因此您可以进一步缩短它:)

Database.arrayList.sort((o1, o2) -> o1.getStartDate().compareTo(o2.getStartDate()));

This is such a common idiom that there's a built-in method to generate a Comparator for a class with a Comparable key:

(这是一个非常常见的习惯用法,因此有一个内置方法可以为具有Comparable键的类生成Comparator :)

Database.arrayList.sort(Comparator.comparing(MyObject::getStartDate));

All of these are equivalent forms.

(所有这些都是等效形式。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...