Sort ArrayList Of Object

Sort ArrayList Of Object Example describes about How to sort an ArrayList of objects.

How to Sort An ArrayList Of Objects Using Comparator Interface

The best way to sort an ArrayList of Objects is using Collections.sort() method of Collections utility class.

But if you need to sort according to a particular field / column / property, then we can use Comparator interface and override compare method of Comparator interface.

Consider an ArrayList consists with lot of student objects, where student object have 2 properties like firstName & lastName. And you need to sort according to the firstName, then you can use comparator interface for sorting the ArrayList

int compare(T o1, T o2) Compares its two arguments for order.