Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

I wanted to sort an ArrayList of custom objects by one of their properties: a Date object (getStartDay()). Normally I compare them by item1.getStartDate().before(item2.getStartDate()) so I was wondering whether I could write something like:

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);     ... } 
like image 888
Samuel Avatar asked May 06 '10 21:05

Samuel


People also ask

How do you sort an array of objects based on a property in Java?

To sort an Object by its property, you have to make the Object implement the Comparable interface and override the compareTo() method. Lets see the new Fruit class again. The new Fruit class implemented the Comparable interface, and overrided the compareTo() method to compare its quantity property in ascending order.

How do you sort elements in an ArrayList using comparable interface?

We can simply implement Comparator without affecting the original User-defined class. To sort an ArrayList using Comparator we need to override the compare() method provided by comparator interface. After rewriting the compare() method we need to call collections. sort() method like below.


1 Answers

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

So your custom Comparator could look like this:

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.

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 java-8

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

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:

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:

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

All of these are equivalent forms.

like image 145
Michael Myers Avatar answered Oct 07 '22 11:10

Michael Myers