How to Sort a List By Using Comparable Interface

How to Sort a List By Using Comparable Interface

In this article, going to explain How to Sort a List By Using Comparable Interface. Wrapper Class type List can be sorted by using the Collection.sort() method but the List type is Object then we have to use comparable interface to sort.

Example

  1. Create VO Class by implementing Comparable interface — StudentVo.java
  2. Create Data generated Class  — StudentDataCreate.java
  3. Create List Sorting main class — ComparableSorter.java

StudentVo.java

Implement Comparable interface and override the compareTo() method and highlighted in bold.

package com.narayanatutorial.ComparableSorting;

public class StudentVo implements Comparable<StudentVo> {

	String studentFirstName;
	String studentLastName;
	String studentCity;
	private int age;

	public String getStudentFirstName() {
		return studentFirstName;
	}

	public void setStudentFirstName(String studentFirstName) {
		this.studentFirstName = studentFirstName;
	}

	public String getStudentLastName() {
		return studentLastName;
	}

	public void setStudentLastName(String studentLastName) {
		this.studentLastName = studentLastName;
	}

	public String getStudentCity() {
		return studentCity;
	}

	public void setStudentCity(String studentCity) {
		this.studentCity = studentCity;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public int compareTo(StudentVo o) {
		int compareAge = o.getAge();

		// ascending order
		// return this.age-compareAge;

		// descending order
		return compareAge - this.age;
	}
}

 

StudentDataCreate.java

package com.narayanatutorial.ComparableSorting;

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

public class StudentDataCreate {
	private List<StudentVo> list;

	public List<StudentVo> getStudentData() {

		list = new ArrayList<StudentVo>();
		StudentVo st = null;
		st = new StudentVo();
		st.setStudentCity("Bangalore");
		st.setStudentFirstName("Narayana1");
		st.setStudentLastName("Ragi1");
		st.setAge(50);
		list.add(st);

		st = new StudentVo();
		st.setStudentCity("Bangalore");
		st.setStudentFirstName("Chandra");
		st.setStudentLastName("Sekhar");
		st.setAge(20);
		list.add(st);

		st = new StudentVo();
		st.setStudentCity("Chennai");
		st.setStudentFirstName("Ravi");
		st.setStudentLastName("Kumar2");
		st.setAge(70);
		list.add(st);

		st = new StudentVo();
		st.setStudentCity("Mumbai");
		st.setStudentFirstName("Ravi");
		st.setStudentLastName("Kumar1");
		st.setAge(37);
		list.add(st);

		return list;
	}
}

 

ComparableSorter.java

Main class to execute.

package com.narayanatutorial.ComparableSorting;

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

public class ComparableSorter {
	public static void main(String args[]) {
		StudentDataCreate sdc = new StudentDataCreate();

		List<StudentVo> list = sdc.getStudentData();
		System.out.println("Before Age Sort \n***********************************");
		System.out.println("FirstName # LastName # City # Age\n***********************************");
		list.forEach(x -> System.out.println(x.getStudentFirstName() + " # " + x.getStudentLastName() + " # "
				+ x.getStudentCity() + " # " + x.getAge()));

		Collections.sort(list);

		System.out.println("\nAfter Age Sort \n***********************************");

		System.out.println("FirstName # LastName # City # Age\n***********************************");

		list.forEach(x -> System.out.println(x.getStudentFirstName() + " # " + x.getStudentLastName() + " # "
				+ x.getStudentCity() + " # " + x.getAge()));

	}
}

 

Output

Before Age Sort 
***********************************
FirstName # LastName # City # Age
***********************************
Narayana1 # Ragi1 # Bangalore # 50
Chandra # Sekhar # Bangalore # 20
Ravi # Kumar2 # Chennai # 70
Ravi # Kumar1 # Mumbai # 37

After Age Sort by Descending 
***********************************
FirstName # LastName # City # Age
***********************************
Ravi # Kumar2 # Chennai # 70
Narayana1 # Ragi1 # Bangalore # 50
Ravi # Kumar1 # Mumbai # 37
Chandra # Sekhar # Bangalore # 20

 

Get full source code from Git.

 

 

 

Leave a Reply