Sorting lists using lambda and comparators in Java 8

Sorting a list collection is one of the most common operation. Prior to Java 8 we had Collections.sort which took list as a parameter and sorted it. In Java 8 this method is present in list class also along with many variations and helpers. We will go through each of them

1. Sorting in natural order

List cities = Arrays.asList("bangalore", "ahemdabad",
				"Calcutta");
		cities.sort(Comparator.naturalOrder());
		System.out.println(cities);//[Calcutta, ahemdabad, bangalore]

2. Sorting in Case insensitive order

List cities = Arrays.asList("bangalore", "ahemdabad",
				"Calcutta");
		cities.sort(String.CASE_INSENSITIVE_ORDER);
		System.out.println(cities);//[ahemdabad, bangalore, Calcutta]

Lets sort complex objects now. Here we define a person class

class person{
 
	@Override
	public String toString() {
		return "person [firstname=" + firstname + ", lastname=" + lastname
				+ ", age=" + age + "]";
	}
 
	String firstname;
 
	String lastname;
 
	int age;
 
	public person(String fn,String ln, int age){
 
		this.firstname = fn;
		this.lastname = ln;
		this.age = age;
	}
 
	public String getFirstname() {
		return firstname;
	}
 
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}
 
	public String getLastname() {
		return lastname;
	}
 
	public void setLastname(String lastname) {
		this.lastname = lastname;
	}
 
	public int getAge() {
		return age;
	}
 
	public void setAge(int age) {
		this.age = age;
	}
}

Lets add some data

List<person> persons = new ArrayList<person>();
		persons.add(new person("uday", "ogra",28));
		persons.add(new person("shikha", "sharma",30));
		persons.add(new person("usha", "ogra",29));

3. Sorting objects by a field

persons.sort(Comparator.comparing(person::getFirstname));
		System.out.println(persons);//[person [firstname=shikha, lastname=sharma, age=30], person [firstname=uday, lastname=ogra, age=28], person [firstname=usha, lastname=ogra, age=29]]

4. Sorting in reverse direction

persons.sort(Comparator.comparing(person::getAge).reversed());
		System.out.println(persons);//[person [firstname=shikha, lastname=sharma, age=30], person [firstname=usha, lastname=ogra, age=29], person [firstname=uday, lastname=ogra, age=28]]

5. Sorting objects by multiple fields

persons.sort(Comparator.comparing(person::getLastname).thenComparing(person::getAge));
		System.out.println(persons);[person [firstname=uday, lastname=ogra, age=28], person [firstname=usha, lastname=ogra, age=29], person [firstname=shikha, lastname=sharma, age=30]]

6. Sorting objects with null field

persons.add(null);
persons.sort(Comparator.nullsFirst(Comparator.comparing(person::getFirstname)));
		System.out.println(persons);//[null, person [firstname=shikha, lastname=sharma, age=30], person [firstname=uday, lastname=ogra, age=28], person [firstname=usha, lastname=ogra, age=29]]


Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *