forum

Home / DeveloperSection / Forums / Sort ArrayList of custom Objects by property

Sort ArrayList of custom Objects by property

Anonymous User 2027 10-Jul-2015
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);
    ...
}

Updated on 10-Jul-2015
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By