How to sort list of objects in Java. Sorting Objects in Java

Sorting:

Usually if you want to sort array of integers or string it is quite straight forward like Arrays.sort(myIntArray) or Collections.sort( list ); But say if you want to sort a list/array of objects then its not straight forward.

In this tutorial we would see how to sort a list of objects java. There are two ways of sorting objects in java, by using Comparable or Comparator. Its always confusing for people to use which over the other and why?

Here are my suggestions.

  • If your object is to be sorted only by single attribute then go for comparable. The class that need’s to be sorted should implement comparable and define compareTo(Object arg0) method.
  • If your object is to be sorted on multiple attribute’s then go for comparator which takes two objects to compare against each other.

 

 

Example: Consider you have a person class whose attributes are firstName, lastName, age, location.

Syntax: Sorting objects based on string property.

Collections.sort(listOfObjectsToBeSorted, new Comparator<ObjectType>() {
public int compare(Object o1, Object o2) {
return o1.getLocation().compareToIgnoreCase(o2.getLocation()); // Sorting based on Location                                                                                                                                             // which is of string type.
}
});

Syntax: Sorting objects based on integer property.

Collections.sort(listOfObjectsToBeSorted, new Comparator<ObjectType>() {
public int compare(Object o1, Object o2) {
return 01.getAge() – o2.getAge(); // Sorting based on age which is integer type.
}

In the following program we would see how you could sort by each property.

public class Person {

private String firstName;
private String lastName;
private int age;
private String location;

public Person(String firstName, String lastName, int age, String location) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.location = location;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public int getAge() {
return age;
}

public String getLocation() {
return location;
}

@Override
public String toString() {
return “Person [firstName=” + firstName + “, lastName=” + lastName + “, age=” + age + “, location=” + location + “]”;
}

public static void main(String args[]) {
Person person1 = new Person(“Aaron”, “James”, 24, “San Jose”);
Person person2 = new Person(“Bart”, “Modi”, 34, “San Fransisco”);
Person person3 = new Person(“Cathay”, “Pacific”, 22, “Atlanta”);

List<Person> personsList = new ArrayList<Person>();

personsList.add(person1);
personsList.add(person2);
personsList.add(person3);

//Sort According to First Name.
System.out.println(sortObjects(personsList, “firstName”));

//Sort According to Location.
System.out.println(sortObjects(personsList, “location”));

//Sort According to First Name.
System.out.println(sortObjects(personsList, “age”));

}

public static List<Person> sortObjects(List<Person> personsList, String sortParameter) {

if (sortParameter.equalsIgnoreCase(“location”)) {

if (personsList != null && personsList.size() > 0)
Collections.sort(personsList, new Comparator<Person>() {
public int compare(Person p1, Person p2) {
return p1.getLocation().compareToIgnoreCase(p2.getLocation());
}
});

} else if (sortParameter.equalsIgnoreCase(“firstName”)) {
if (personsList != null && personsList.size() > 0)
Collections.sort(personsList, new Comparator<Person>() {
public int compare(Person p1, Person p2) {
return p1.getFirstName().compareToIgnoreCase(p2.getFirstName());
}
});
}
//Sort by age.
else {

if (personsList != null && personsList.size() > 0)
Collections.sort(personsList, new Comparator<Person>() {
public int compare(Person p1, Person p2) {
return p1.getAge() – p2.getAge();
}
});
}

return personsList;
}

}

 

 

About Tharun Tej

Tharun is currently an Engineer @ eBay Inc. Tharun has experience in the areas of mobile, cloud and back-end development. He is passionate about business strategies, product development, leadership and people. Check out his Blog.

Speak Your Mind

*