Tag Archives: Sorting

Sorting Java Custom Objects using Comparator, Comparable and Java Collections

Sorting a List of Custom Java Ojects using Comparable and Java Collections

Assume that we have list of customer objects ( instance of Customer class, the code snippet is below) in an ArrayList and need sorting the list of objects using customer’s age.
The below code snippet shows how..

package com.sort;
public class Customer implements Comparable{
	private Integer age;
	private String name;
	public Customer(Integer age, String name) {
	     this.age = age;
	     this.name = name;
	}
	@Override
	public int compareTo(Customer cust) {
		return age.compareTo(cust.age);
	}
	@Override
	public String toString() {
		return " "+ name +" " + age;
	}
// Getters and setters for age, name
}

package com.sort;

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

public class CustomersSort {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		List custList = new ArrayList();
		
		custList.add(new Customer(23, "Roger"));
		custList.add(new Customer(17, "Kuldip"));
		custList.add(new Customer(20, "Jyo"));
		
		System.out.println(custList);
		
		Collections.sort(custList);
	
	        System.out.println(custList);
		
	}

}

Output from the above program

Line Number 1: shows the output before the sort
Line Number 2: shows the output after the sort


1. [ Roger 23,  Kuldip 17,  Jyo 20]
2. [ Kuldip 17,  Jyo 20,  Roger 23]

Use of Comparator

How do we achieve if we need to sort the customers using name instead of Age?

We can either go to the Customer class and update the compareTo method to use the Customer Name as below

        @Override
	public int compareTo(Customer cust) {
		return name.compareTo(cust.name);
	}

OR

Use the Comparator. This is the best option because we some times needs to have sorting by age and name as well. The code snippet on how to use the Comparator is below


package com.sort;

import java.util.Comparator;

public class CustNameSort implements Comparator{

	@Override
	public int compare(Customer cust1, Customer cust2) {
	
		return cust1.getName().compareTo(cust2.getName());
	}
	
	

Now sort using the java Collections framework.

Collections.sort(custList,new CustNameSort());


Sorting ArrayList using Java Collections framework

Sorting ArrayList using Collections


/**
 * 
 */
package com.sort;

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

/**
 * @author CodeDairy
 *
 */
public class ArrayListSorting {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		List arrayList = new ArrayList();
		
		arrayList.add("java");
		arrayList.add("sort");
		arrayList.add("array");
		
		System.out.println(arrayList);
		Collections.sort(arrayList);
		System.out.println(arrayList);
		
	}

}

Out of the above program

Line No.1 : shows the output before the sort
Line No.2 : shows the output after the sort


1. [java, sort, array]
2. [array, java, sort]