Sort a List in Java Example

Sort a List in Java Example

List being widely used in Data Structure, has its own importance. List is used to store elements with its own defined methods. Let’s see how we can sort data stored in List. To learn more about List click here.

Sort a List containing Integer

To sort an List, use Collections.sort() method.

package com.code2succeed.sorting.list;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ListSortingExample {
	public static void main(String[] args) {
		List<Integer> list = new ArrayList<Integer>();
		list.add(12);
		list.add(32);
		list.add(43);
		list.add(10);
		list.add(5);
		list.add(23);
		list.add(99);
		list.add(1);
		
		System.out.println("List before sorting : "+list);
		Collections.sort(list);
		System.out.println("List after sorting : "+list);
	}
}

Output

List before sorting : [12, 32, 43, 10, 5, 23, 99, 1]
List after sorting : [1, 5, 10, 12, 23, 32, 43, 99]

Sort a List containing String

We can use Collections.sort() method just like above example to sort a List containing String.

package com.code2succeed.sorting.list;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ListSortingExample {
	public static void main(String[] args) {			
		List<String> strList = new ArrayList<String>();
		strList.add("abc");
		strList.add("xyz"); 
		strList.add("aaa");
		strList.add("bca");
		strList.add("pqr");
		strList.add("stu");
		strList.add("xxx");
		
		System.out.println("List before sorting : "+strList);
		Collections.sort(strList);
		System.out.println("List after sorting : "+strList);
	}
}

Output

List before sorting : [abc, xyz, aaa, bca, pqr, stu, xxx]
List after sorting : [aaa, abc, bca, pqr, stu, xxx, xyz]

Sort a List containing custom objects as element using comparable.

In this example we have created one custom class Emp with firstName, lastName and age as instance variable. Emp class implements Comparable interface and sort Emp objects based on age.

Emp bean class
package com.code2succeed.sorting.comparable;

@SuppressWarnings("rawtypes")
public class Emp implements Comparable<Emp> {
	private String firstName;
	private String lastName;
	private int age;
	
	public Emp(){}
	public Emp(String firstName, String lastName, int age) {
		this.firstName = firstName;
		this.lastName = lastName;
		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;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("\nFirstName :"+firstName);
		sb.append(", LastName : "+lastName);
		sb.append(", age : "+age);
		
		return sb.toString();
	}
	@Override
	public int compareTo(Emp emp) {
		return this.age - emp.age;
	}
}
Main method to sort a List
package com.code2succeed.sorting.list;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.code2succeed.sorting.comparable.Emp;

public class ListComparableExample {
	public static void main(String[] args) {
		List<Emp> emps = new ArrayList<Emp>();
		
		emps.add(new Emp("Charles", "Anderson", 34));
		emps.add(new Emp("Rob", "Anderson", 31));
		emps.add(new Emp("Sheen", "Chan", 45));
		emps.add(new Emp("Rahul", "Mahadewan", 36));
		emps.add(new Emp("Mona", "J", 25));
		
		System.out.println("List before sorting : "+emps);
		Collections.sort(emps);
		
		System.out.println("List after sorting : "+emps);
	}
}

Output

List before sorting : [
FirstName :Charles, LastName : Anderson, age : 34, 
FirstName :Rob, LastName : Anderson, age : 31, 
FirstName :Sheen, LastName : Chan, age : 45, 
FirstName :Rahul, LastName : Mahadewan, age : 36, 
FirstName :Mona, LastName : J, age : 25]

List after sorting : [
FirstName :Mona, LastName : J, age : 25, 
FirstName :Rob, LastName : Anderson, age : 31, 
FirstName :Charles, LastName : Anderson, age : 34, 
FirstName :Rahul, LastName : Mahadewan, age : 36, 
FirstName :Sheen, LastName : Chan, age : 45]

We have sorted the Array which contains custom object as elements using Comparable. In Emp class, we have used “age” as a parameter to sort the elements of the array. But, what if we want to sort the array using other parameters for example, based on “firstName”. So, by using Comparable we can only sort elements on one parameter. To sort objects on different parameter we should use Comparator interface. Here is example to sort an array using Comparator interface using “firstName” as sort criteria.

Sort a List containing custom objects as element using Comparator

package com.code2succeed.sorting.list;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.code2succeed.sorting.comparable.Emp;

public class ListComparatorExample {
	public static void main(String[] args) {
		List<Emp> emps = new ArrayList<Emp>();
		
		emps.add(new Emp("Charles", "Anderson", 34));
		emps.add(new Emp("Rob", "Anderson", 31));
		emps.add(new Emp("Sheen", "Chan", 45));
		emps.add(new Emp("Rahul", "Mahadewan", 36));
		emps.add(new Emp("Mona", "J", 25));
		
		System.out.println("List before sorting : "+emps);
		Collections.sort(emps, new EmpComparator());
		
		System.out.println("List after sorting : "+emps);
	}
}

class EmpComparator implements Comparator<Emp> {
	@Override
	public int compare(Emp emp1, Emp emp2) {
		return emp1.getFirstName().compareToIgnoreCase(emp2.getFirstName());
	}
}

Output

List before sorting : [
FirstName :Charles, LastName : Anderson, age : 34, 
FirstName :Rob, LastName : Anderson, age : 31, 
FirstName :Sheen, LastName : Chan, age : 45, 
FirstName :Rahul, LastName : Mahadewan, age : 36, 
FirstName :Mona, LastName : J, age : 25]

List after sorting : [
FirstName :Charles, LastName : Anderson, age : 34, 
FirstName :Mona, LastName : J, age : 25, 
FirstName :Rahul, LastName : Mahadewan, age : 36, 
FirstName :Rob, LastName : Anderson, age : 31, 
FirstName :Sheen, LastName : Chan, age : 45]

To Sort an Array in java click here.
To Sort a Map/HashMap in java click here.

 

Leave a Reply

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