How to sort list in java example

List can be sorted by either of following three ways.

  • java.util.Collections.sort()
  • Using Comparable interface
  • Using Comparator interface

Example of Collection Sorting

Here I am taking example to sort ArrayList using Collections.sort() method.


package com.startwithjava.collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class ArrayListTest {

public static void main(String args[]){
ArrayList<String> list = new ArrayList<String>();
list.add("Project A");
list.add("Project C");
list.add(1,"Project B");
list.add("Project D");
list.add("Project E");
Collections.sort(list);

//Iterate ArrayList using Iterator
Iterator itr = list.iterator();
int counter=0;
while(itr.hasNext()){
String str = (String)itr.next();
System.out.println("Element at pos "+counter+" is "+str);
counter++;
}

}

}

Using Comparable interface

Comparable interface is used to sort the objects of user-defined class.This interface has compareTo(Object) method.

Syntax

public int compareTo(Object obj): It is used to compare the current object with the specified object.

Employee.java

package com.startwithjava.collections.beans;

public class Employee implements Comparable{
 private int id;
 private String name;
 private double salary;
public int getId() {
	return id;
}

public Employee(int id, String name, double salary) {
	super();
	this.id = id;
	this.name = name;
	this.salary = salary;
}

public void setId(int id) {
	this.id = id;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public double getSalary() {
	return salary;
}
public void setSalary(double salary) {
	this.salary = salary;
}
public int compareTo(Object obj){  
	Employee emp=(Employee)obj;  
	if(salary==emp.salary)  
	return 0;  
	else if(salary>emp.salary)  
	return 1;  
	else  
	return -1;  
}  
 
}

ComparableTest.java

package com.startwithjava.collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import com.startwithjava.collections.beans.Employee;

public class ComparableTest {
	public static void main(String args[]){
		ArrayList<Employee> list = new ArrayList<Employee> ();
		Employee e1 = new Employee(1,"Raj Kumar",62000);
		Employee e2 = new Employee(2,"Ram Kumar",8500);
		Employee e3 = new Employee(3,"Sumit Kumar",10000);
		list.add(e1);
		list.add(e2);
		list.add(e3);
		Collections.sort(list);
		
		Iterator itr = list.iterator();
		while(itr.hasNext()){	
			Employee e = (Employee)itr.next();
			System.out.println("Id:"+e.getId()+"\tName:"+e.getName()+"\t"+e.getSalary());
		}
	}
}

Using Comparator interface

Comparator interface is used to sort the objects of user-defined class.The interface has following two methods.We can sort list based on any number of data members like by salary,name,department etc.

  • public int compare(Object obj1,Object obj2)
  • public int equals(Object element)

Here I am going to sort Product objects by PriceComparator based on price data member.

Product.java


package com.startwithjava.collections.beans;
import java.util.Comparator;
public class Product{
private int id;
private String name;
private int price;

public Product() {
super();
}
public Product(int id, String name, int price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}

}

PriceComparator.java


package com.startwithjava.collections.comparator;
import java.util.Comparator;
import com.startwithjava.collections.beans.Product;
public class PriceComparator implements Comparator<Product>{
public int compare(Product p1,Product p2){
return p1.getPrice()-p2.getPrice();
}
}

ComparatorTest.java


package com.startwithjava.collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import com.startwithjava.collections.beans.Product;
public class ComparatorTest {
public static void main(String args[]){
Product p1 = new Product(1, "Product A", 7000);
Product p2 = new Product(2, "Product B", 5000);
Product p3 = new Product(3, "Product C", 10000);
ArrayList<Product> list = new ArrayList<Product>();
list.add(p1);
list.add(p2);
list.add(p3);
Collections.sort(list, new PriceComparator());
Iterator<Product> itr = list.iterator();
while(itr.hasNext()){
Product p = itr.next();
System.out.println("Product Id:"+p.getId()+"\tName:"+p.getName());
}
}
}

Leave a Reply