There are two ways to sort list in java -
- Sort list of String/Wrapper Class (Using Default Soring)
- Sort list of User-Defined Class (Using Custom Soring)
Sort list of String/Wrapper Class
A list of String/Wrapper Class (Integer, Long, Byte, Double, Float, Short) can be sorted using utility class Collections present in java.util package. This class contains a static method sort(List<T> list) that takes a list and sorts it into ascending order. If you don't want the default sorting order you can use the overloaded version of sort method sort(List<T> list, Comparator<? super T> c) that takes a list and a comparator and sorts that list using the given comparator. Let us now see some examples -
Example 1 : Sort List of a String
package com.codemeright.sortList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SortStringList {
public static void main(String[] args) {
//Create a list of String that you want to sort. Your list might come from database.
List<String> stringList = new ArrayList<String>();
stringList.add("Right");
stringList.add("Me");
stringList.add("Code");
//Print list before sorting
System.out.println("Before Sorting : ");
System.out.println("----------------");
for(String s : stringList) {
System.out.println(s);
}
//Sort list in ascending order
Collections.sort(stringList);
//Print list after sorting
System.out.println("After Sorting : ");
System.out.println("----------------");
for(String s : stringList) {
System.out.println(s);
}
}
}
OUTPUT:
Before Sorting :
----------------
Right
Me
Code
After Sorting :
----------------
Code
Me
Right
Example 2 : Sort List of an Integer
package com.codemeright.sortList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SortIntegerList {
public static void main(String[] args) {
//Create a list of String that you want to sort. Your list might come from database.
List<Integer> integerList = new ArrayList<Integer>();
integerList.add(52);
integerList.add(453);
integerList.add(76);
//Print list before sorting
System.out.println("Before Sorting : ");
System.out.println("----------------");
for (Integer i : integerList) {
System.out.println(i);
}
//Sort list in ascending order
Collections.sort(integerList);
//Print list after sorting
System.out.println("After Sorting : ");
System.out.println("----------------");
for (Integer i : integerList) {
System.out.println(i);
}
}
}
OUTPUT:
Before Sorting :
----------------
52
453
76
After Sorting :
----------------
52
76
453
A list of any wrapper class can be sorted same as the above example. But if you don't want the default sorting order and want to implement custom sorting order then you can use overloaded sort method of Collections Class that takes a list and a comparator. For implementation of custom sorting, you can refer to the examples below.
Sort list of User-Defined Class
For example, I have a list of Employee Class. The employee class contains name, gender, age, dob and salary properties. Hence, the list is representing a tabular data like the data shown in the table below. This data can be sorted by name, gender, age, dob or salary. Let us try to understand how to achieve that with some examples -
Name | Gender | Age | Date of Birth | Salary (Lacs per annum) |
Hugh Jackman | Male | 48 | 12-10-1968 | 1.88 |
Angelina Jolie | Female | 42 | 04-06-1975 | 1.69 |
Megan Fox | Female | 31 | 16-05-1986 | 1.63 |
Dwayne Johnson | Male | 45 | 02-05-1972 | 1.96 |
Scarlett Johansson | Female | 32 | 22-11-1984 | 1.60 |
Example 3 : Sort List by String Column 'Name'
package com.codemeright.sortList;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class SortListByStringColumn {
public static void main(String[] args) throws ParseException {
//Create a list that you want to sort. Your list might come from database.
List<Employee> employees = new ArrayList<>();
employees.add(new Employee("Hugh Jackman", Gender.Male, 48, new SimpleDateFormat("dd-MM-yyyy").parse("12-10-1968"), 1.88));
employees.add(new Employee("Angelina Jolie", Gender.Female, 42, new SimpleDateFormat("dd-MM-yyyy").parse("04-06-1975"), 1.69));
employees.add(new Employee("Megan Fox", Gender.Female, 31, new SimpleDateFormat("dd-MM-yyyy").parse("16-05-1986"), 1.63));
employees.add(new Employee("Dwayne Johnson", Gender.Male, 45, new SimpleDateFormat("dd-MM-yyyy").parse("02-05-1972"), 1.96));
employees.add(new Employee("Scarlett Johansson", Gender.Male, 32, new SimpleDateFormat("dd-MM-yyyy").parse("22-11-1984"), 1.60));
//Print list before sorting
System.out.println("Before Sorting By Name : ");
System.out.println("------------------------");
for (Employee e : employees) {
System.out.println(e.getName());
}
//Sort list by employee name
employees.sort(new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
//Return 0 if o1 and o2 are equal
//Return +1 if o1 comes after o2
//Return -1 if o1 comes before o2
if (o1 == o2) {
return 0;
}
if (o1 != null) {
return (o2 != null) ? o1.getName().compareTo(o2.getName()) : 1;
}
return -1;
}
});
//Print list after sorting
System.out.println("After Sorting By Name : ");
System.out.println("------------------------");
for (Employee e : employees) {
System.out.println(e.getName());
}
}
}
OUTPUT:
Before Sorting By Name :
------------------------
Hugh Jackman
Angelina Jolie
Megan Fox
Dwayne Johnson
Scarlett Johansson
After Sorting By Name :
------------------------
Angelina Jolie
Dwayne Johnson
Hugh Jackman
Megan Fox
Scarlett Johansson
Example 4 : Sort List by Integer Column 'Age'
package com.codemeright.sortList;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class SortListByIntegerColumn {
public static void main(String[] args) throws ParseException {
//Create a list that you want to sort. Your list might come from database.
List<Employee> employees = new ArrayList<>();
employees.add(new Employee("Hugh Jackman", Gender.Male, 48, new SimpleDateFormat("dd-MM-yyyy").parse("12-10-1968"), 1.88));
employees.add(new Employee("Angelina Jolie", Gender.Female, 42, new SimpleDateFormat("dd-MM-yyyy").parse("04-06-1975"), 1.69));
employees.add(new Employee("Megan Fox", Gender.Female, 31, new SimpleDateFormat("dd-MM-yyyy").parse("16-05-1986"), 1.63));
employees.add(new Employee("Dwayne Johnson", Gender.Male, 45, new SimpleDateFormat("dd-MM-yyyy").parse("02-05-1972"), 1.96));
employees.add(new Employee("Scarlett Johansson", Gender.Male, 32, new SimpleDateFormat("dd-MM-yyyy").parse("22-11-1984"), 1.60));
//Print list before sorting
System.out.println("Before Sorting By Age : ");
System.out.println("------------------------");
for (Employee e : employees) {
System.out.println(e.getName() + " (" + e.getAge() + ")");
}
//Sort list by employee name
employees.sort(new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
//Return 0 if o1 and o2 are equal
//Return +1 if o1 comes after o2
//Return -1 if o1 comes before o2
if (o1 == o2) {
return 0;
}
if (o1 != null) {
return (o2 != null) ? Integer.compare(o1.getAge(), o2.getAge()) : 1;
}
return -1;
}
});
//Print list after sorting
System.out.println("After Sorting By Age : ");
System.out.println("------------------------");
for (Employee e : employees) {
System.out.println(e.getName() + " (" + e.getAge() + ")");
}
}
}
OUTPUT:
Before Sorting By Age :
------------------------
Hugh Jackman (48)
Angelina Jolie (42)
Megan Fox (31)
Dwayne Johnson (45)
Scarlett Johansson (32)
After Sorting By Age :
------------------------
Megan Fox (31)
Scarlett Johansson (32)
Angelina Jolie (42)
Dwayne Johnson (45)
Hugh Jackman (48)
Example 5 : Sort List by Double Column 'Salary'
package com.codemeright.sortList;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class SortListByDoubleColumn {
public static void main(String[] args) throws ParseException {
//Create a list that you want to sort. Your list might come from database.
List<Employee> employees = new ArrayList<>();
employees.add(new Employee("Hugh Jackman", Gender.Male, 48, new SimpleDateFormat("dd-MM-yyyy").parse("12-10-1968"), 1.88));
employees.add(new Employee("Angelina Jolie", Gender.Female, 42, new SimpleDateFormat("dd-MM-yyyy").parse("04-06-1975"), 1.69));
employees.add(new Employee("Megan Fox", Gender.Female, 31, new SimpleDateFormat("dd-MM-yyyy").parse("16-05-1986"), 1.63));
employees.add(new Employee("Dwayne Johnson", Gender.Male, 45, new SimpleDateFormat("dd-MM-yyyy").parse("02-05-1972"), 1.96));
employees.add(new Employee("Scarlett Johansson", Gender.Male, 32, new SimpleDateFormat("dd-MM-yyyy").parse("22-11-1984"), 1.60));
//Print list before sorting
System.out.println("Before Sorting By Salary : ");
System.out.println("------------------------");
for (Employee e : employees) {
System.out.println(e.getName() + " (" + e.getSalary() + ")");
}
//Sort list by employee name
employees.sort(new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
//Return 0 if o1 and o2 are equal
//Return +1 if o1 comes after o2
//Return -1 if o1 comes before o2
if (o1 == o2) {
return 0;
}
if (o1 != null) {
return (o2 != null) ? Double.compare(o1.getSalary(), o2.getSalary()) : 1;
}
return -1;
}
});
//Print list after sorting
System.out.println("After Sorting By Salary : ");
System.out.println("------------------------");
for (Employee e : employees) {
System.out.println(e.getName() + " (" + e.getSalary() + ")");
}
}
}
OUTPUT:
Before Sorting By Salary :
------------------------
Hugh Jackman (1.88)
Angelina Jolie (1.69)
Megan Fox (1.63)
Dwayne Johnson (1.96)
Scarlett Johansson (1.6)
After Sorting By Salary :
------------------------
Scarlett Johansson (1.6)
Megan Fox (1.63)
Angelina Jolie (1.69)
Hugh Jackman (1.88)
Dwayne Johnson (1.96)
Example 5 : Sort List by Date Column 'DOB'
package com.codemeright.sortList;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class SortListByDateColumn {
public static void main(String[] args) throws ParseException {
//Create a list that you want to sort. Your list might come from database.
List<Employee> employees = new ArrayList<>();
employees.add(new Employee("Hugh Jackman", Gender.Male, 48, new SimpleDateFormat("dd-MM-yyyy").parse("12-10-1968"), 1.88));
employees.add(new Employee("Angelina Jolie", Gender.Female, 42, new SimpleDateFormat("dd-MM-yyyy").parse("04-06-1975"), 1.69));
employees.add(new Employee("Megan Fox", Gender.Female, 31, new SimpleDateFormat("dd-MM-yyyy").parse("16-05-1986"), 1.63));
employees.add(new Employee("Dwayne Johnson", Gender.Male, 45, new SimpleDateFormat("dd-MM-yyyy").parse("02-05-1972"), 1.96));
employees.add(new Employee("Scarlett Johansson", Gender.Male, 32, new SimpleDateFormat("dd-MM-yyyy").parse("22-11-1984"), 1.60));
//Print list before sorting
System.out.println("Before Sorting By DOB : ");
System.out.println("------------------------");
for (Employee e : employees) {
System.out.println(e.getName() + " (" + new SimpleDateFormat("dd-MM-yyyy").format(e.getDob()) + ")");
}
//Sort list by employee name
employees.sort(new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
//Return 0 if o1 and o2 are equal
//Return +1 if o1 comes after o2
//Return -1 if o1 comes before o2
if (o1 == o2) {
return 0;
}
if (o1 != null) {
return (o2 != null) ? o1.getDob().compareTo(o2.getDob()) : 1;
}
return -1;
}
});
//Print list after sorting
System.out.println("After Sorting By DOB : ");
System.out.println("------------------------");
for (Employee e : employees) {
System.out.println(e.getName() + " (" + new SimpleDateFormat("dd-MM-yyyy").format(e.getDob()) + ")");
}
}
}
OUTPUT:
Before Sorting By DOB :
------------------------
Hugh Jackman (12-10-1968)
Angelina Jolie (04-06-1975)
Megan Fox (16-05-1986)
Dwayne Johnson (02-05-1972)
Scarlett Johansson (22-11-1984)
After Sorting By DOB :
------------------------
Hugh Jackman (12-10-1968)
Dwayne Johnson (02-05-1972)
Angelina Jolie (04-06-1975)
Scarlett Johansson (22-11-1984)
Megan Fox (16-05-1986)
So, that is how you can sort a List of String/Wrapper Classes & Custom Classes in ascending order using sort(List<T> list) method of Collections Class and if you need custom sorting then you can use overloaded method sort(List<T> list, Comparator<? super T> c). I hope you will find it easy to understand and simple to implement.
0 Comments