This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details
Sort the ArrayList of custom objects by property?
I’ve read about sorting arrayLists using comparators, but compareTo of all the examples that people use, according to some research, is a method of String.
I want to sort the ArrayList by one of the attributes of the custom object: the Date object (getStartDay()). In general, I compare them, item1.getStartDate().before(item2.getStartDate()) so I wonder if I can write something like the following:
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); . }Copy the code
Answer:
Comparable, since the Date implementation, has the same method String as the compareTo method.
Therefore, your custom setting Comparator might look like this:
public class CustomComparator implements Comparator<MyObject> { @Override public int compare(MyObject o1, MyObject o2) { return o1.getStartDate().compareTo(o2.getStartDate()); }}Copy the code
The compare() method must return an int, so Boolean can’t just return one that you planned anyway.
Your sorting code is almost as you wrote it:
Collections.sort(Database.arrayList, new CustomComparator());
Copy the code
If you don’t need to reuse the comparator, a shorter way to write all this 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()); }});Copy the code
Since the Java – 8
Now you can write the last example Comparator in a shorter form by using a lambda expression:
Collections.sort(Database.arrayList,
(o1, o2) -> o1.getStartDate().compareTo(o2.getStartDate()));
Copy the code
And List provides a sort(Comparator) method, so you can shorten this time even further:
Database.arrayList.sort((o1, o2) -> o1.getStartDate().compareTo(o2.getStartDate()));
Copy the code
This is a very common idiom, so there is a built-in method for the Comparator to generate one for classes with Comparable keys:
Database.arrayList.sort(Comparator.comparing(MyObject::getStartDate));
Copy the code
All of these are equivalent forms.
The article translated from yl2gl72eozkinivz3vc6swkesy – ac4c6men2g7xr2a – translate. Translate. Goog/questions / 2…
The author suggests: many kinds of ways, good envy!! A simple extension of the Comparable and Comparator calls from Collections#sort() uses the strategy pattern, which essentially means that we implement the provided methods when we want to use them
We can rewrite the sorting algorithm by implements Comparable
Thank you for reading this, if this article is well written and if you feel there is something to it
Ask for a thumbs up 👍 ask for attention ❤️ ask for share 👥 for 8 abs I really very useful!!
If there are any mistakes in this blog, please comment, thank you very much! ❤ ️ ❤ ️ ❤ ️ ❤ ️