How to Sort a List By Using Comparable Interface
Table of Contents
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
- Create VO Class by implementing Comparable interface — StudentVo.java
- Create Data generated Class — StudentDataCreate.java
- 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.
Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in the IT industry for more than 12 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting, Website Development, and IAM(ForgeRock) Specialist
I am a self-learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.