Sort ArrayList of custom Objects by property
By default, the ArrayList's elements are displayed according to the order they are added into the list. You can sort the ArrayList to make it a natural order (alphabetically or numbers in ascending order). The following example shows the use of the Collections.sort(List) to sort an ArrayList.
import java.util.ArrayList;
import java.util.Collections;
public class SortArrayList {
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add("234");
list.add("Tom");
list.add("756");
list.add("Harley");
list.add("123");
list.add("Gloary");
//before the list sort
System.out.println("ArrayList is Before sorted");
for (String string : list) {
System.out.println(string);
}
//sort the list
Collections.sort(list);
//after sorted the list
System.out.println("ArrayList is After sorted");
for (String string : list) {
System.out.println(string);
}
}
}
The OutPout:
ArrayList is Before sorted
234
Tom
756
Harley
123
Gloary
ArrayList is After sorted
123
234
756
Gloary
Harley
Tom
This is possible because String implements the Comparable interface. This interface defines the method compare which performs pairwise comparison of the elements and returns -1 if the element is smaller than the compared element, 0 if it is equal and 1 if it is larger.Classes that has a natural sort order (Ex Number, String etc) should implement the Comparable interface, whilst classes that has no natural sort order (Ex: Person, Employee etc) should be provided with a Comparator class. Here in the below Person class list Collections.sort() is not work, has Person class did not implement the Comparator interface. To sort Person class based on any property, we need to create a sorting class which implements Comparator interface. In this example, I am going to explain how to implement sorting functionality.
Person.java
import java.util.Date;
public class Person {
private int id;
private String name;
private Date dob;
private String phoneNo;
public int getId() {
return id;
}
public Person(){}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public String getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
}
SortPerson.java
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SortPerson implements Comparator {
public int sortId;
public SortPerson(int sortId) {
this.sortId = sortId;
}
public int compare(Object a1, Object a2) {
Person o1 = (Person) a1;
Person o2 = (Person) a2;
try {
if (sortId == 1) {
return o1.getId() - o2.getId();
} else if (sortId == 2) {
return o1.getName().toLowerCase().compareTo(o2.getName().toLowerCase());
} else if (sortId == 3) {
double val1 = Double.parseDouble(o1.getPhoneNo());
double val2 = Double.parseDouble(o2.getPhoneNo());
if (val1 > val2) {
return 1;
} else if (val1 < val2) {
return -1;
} else {
return 0;
}
} else if (sortId == 4) {
return o1.getDob().compareTo(o2.getDob());
} else {
return 0;
}
} catch (Exception ex) {
ex.printStackTrace();
System.err.println("Some Exception has occured in compare() of SortPerson:: " + ex.getMessage());
}
return 0;
}
public List sort(List listPersons) {
Collections.sort(listPersons, new SortPerson(sortId));
return listPersons;
}
}
The SortArrayList.java
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
public class SortArrayList {
public static void main(String args[]) throws Exception {
List list = new ArrayList();
Person person1 = new Person();
person1.setId(5);
person1.setName("Glory");
person1.setDob(new SimpleDateFormat("MM/dd/yyyy").parse("11/09/1981"));
person1.setPhoneNo("2244565438");
list.add(person1);
Person person2 = new Person();
person2.setId(1);
person2.setName("Tom");
person2.setDob(new SimpleDateFormat("MM/dd/yyyy").parse("08/16/1980"));
person2.setPhoneNo("6032198845");
list.add(person2);
Person person3 = new Person();
person3.setId(7);
person3.setName("Harley");
person3.setDob(new SimpleDateFormat("MM/dd/yyyy").parse("04/05/1981"));
person3.setPhoneNo("8036738745");
list.add(person3);
//before sort
System.out.println("ArrayList is Before sort");
for (Person person : list) {
System.out.println(person.getId() + "\t" + person.getName() + "\t" + person.getPhoneNo() + "\t" + person.getDob());
}
//sort the list
//Here the sortId is the property on which we sort.
SortPerson sortPerson = new SortPerson(4);
list = sortPerson.sort(list);
//After sorted the list
System.out.println("ArrayList is After sort");
for (Person person : list) {
System.out.println(person.getId() + "\t" + person.getName() + "\t" + person.getPhoneNo() + "\t" + person.getDob());
}
}
}
Output: The sort Id is Person Id
ArrayList is Before sort
5 Glory 2244565438 Mon Nov 09 00:00:00 IST 1981
1 Tom 6032198845 Sat Aug 16 00:00:00 IST 1980
7 Harley 8036738745 Sun Apr 05 00:00:00 IST 1981
ArrayList is After sort
1 Tom 6032198845 Sat Aug 16 00:00:00 IST 1980
5 Glory 2244565438 Mon Nov 09 00:00:00 IST 1981
7 Harley 8036738745 Sun Apr 05 00:00:00 IST 1981
Output: The sort Id is Person Date of Birth
ArrayList is Before sort
5 Glory 2244565438 Mon Nov 09 00:00:00 IST 1981
1 Tom 6032198845 Sat Aug 16 00:00:00 IST 1980
7 Harley 8036738745 Sun Apr 05 00:00:00 IST 1981
ArrayList is After sort
1 Tom 6032198845 Sat Aug 16 00:00:00 IST 1980
7 Harley 8036738745 Sun Apr 05 00:00:00 IST 1981
5 Glory 2244565438 Mon Nov 09 00:00:00 IST 1981
Leave a Comment