List<String> listStrings = Arrays.asList("Orange", "Grape", "Apple", "Lemon", "Banana"); System.out.println("Before sorting: " + listStrings); Collections.sort(listStrings); System.out.println("After sorting: " + listStrings);
Before sorting: [Orange, Grape, Apple, Lemon, Banana] After sorting: [Apple, Banana, Grape, Lemon, Orange]
List<Character> listCharacters = Arrays.asList('F', 'C', 'A', 'B', 'Z', 'E', 'K', 'P'); System.out.println("Before sorting: " + listCharacters); Collections.sort(listCharacters); System.out.println("After sorting: " + listCharacters);
Before sorting: [F, C, A, B, Z, E, K, P] After sorting: [A, B, C, E, F, K, P, Z]
List<Integer> listIntegers = Arrays.asList(1, 6, 9, 3, 2, 0, 8, 4, 7, 5); System.out.println("Before sorting: " + listIntegers); Collections.sort(listIntegers); System.out.println("After sorting: " + listIntegers);
Before sorting: [1, 6, 9, 3, 2, 0, 8, 4, 7, 5] After sorting: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
List<Date> listDates = new ArrayList<Date>(); DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd"); try { listDates.add(dateFormatter.parse("2013-09-30")); listDates.add(dateFormatter.parse("2013-07-06")); listDates.add(dateFormatter.parse("2013-11-28")); } catch (ParseException ex) { System.err.print(ex); } System.out.println("Before sorting: " + listDates); Collections.sort(listDates); System.out.println("After sorting: " + listDates);
Before sorting: [Mon Sep 30 00:00:00 ICT 2013, Sat Jul 06 00:00:00 ICT 2013, Thu Nov 28 00:00:00 ICT 2013] After sorting: [Sat Jul 06 00:00:00 ICT 2013, Mon Sep 30 00:00:00 ICT 2013, Thu Nov 28 00:00:00 ICT 2013]
List<Integer> listIntegers = Arrays.asList(1, 6, 9, 3, 2, 0, 8, 4, 7, 5); System.out.println("Before sorting: " + listIntegers); Collections.sort(listIntegers); System.out.println("After sorting: " + listIntegers); Collections.reverse(listIntegers); System.out.println("After reversing: " + listIntegers);Output:
Before sorting: [1, 6, 9, 3, 2, 0, 8, 4, 7, 5] After sorting: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] After reversing: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]NOTES: The reverse(list) method doesn’t sort the list, it just re-order the list’s elements in the reverse order. Therefore the list must be sorted using the sort(list) method before being reversed in order to have the list sorted in descending order.
public class Employee { private String name; private int age; private int salary; public Employee(String name, int age, int salary) { this.name = name; this.age = age; this.salary = salary; } // getters and setters }If we try to sort a list whose elements of the type Employeeabove, the sort(list) method will throw a ClassCastException. Now, let’s the Employeeclass implemented the Comparable interface as follows:
public class Employee implements Comparable<Employee> { // variables, getters and setters... @Override public int compareTo(Employee employee) { return employee.salary - this.salary; } }As we see, the Employee class now overrides the compareTo() method from the Comparable interface. This method simply compares the salary between this employee and another. And override the toString() method as follows:
public String toString() { return String.format("(%s, %d, %d)", name, age, salary); }We override the toString() method of the Employeeclass so that the returned string will be used when printing the list content.The following code example creates a list of employees and sorts it based on the descending order of salary:
List<Employee> listEmployees = new ArrayList<Employee>(); listEmployees.add(new Employee("Tom", 45, 80000)); listEmployees.add(new Employee("Sam", 56, 75000)); listEmployees.add(new Employee("Alex", 30, 120000)); listEmployees.add(new Employee("Peter", 25, 60000)); System.out.println("Before sorting: " + listEmployees); Collections.sort(listEmployees); System.out.println("After sorting: " + listEmployees);Output:
Before sorting: [(Tom, 45, 80000), (Sam, 56, 75000), (Alex, 30, 120000), (Peter, 25, 60000)] After sorting: [(Alex, 30, 120000), (Tom, 45, 80000), (Sam, 56, 75000), (Peter, 25, 60000)]See the detailed specification of the compareTo() method.
Collections.sort(list, Comparator)
In this case, the type of the elements need not implement the Comparable interface. This would be useful if we need to sort a list of custom objects which we cannot modify its class; or if we don’t want to rely on the natural ordering of the elements. The following code is example of a comparator that compares two employees based on their ages:package net.codejava.collections; import java.util.Comparator; /** * This comparator compares two employees by their ages. * @author www.codejava.net * */ public class EmployeeAgeComparator implements Comparator<Employee> { @Override public int compare(Employee emp1, Employee emp2) { return emp1.getAge() - emp2.getAge(); } }And the following code snippet sorts a list of employees using the above comparator:
List<Employee> listEmployees = new ArrayList<Employee>(); listEmployees.add(new Employee("Tom", 45, 80000)); listEmployees.add(new Employee("Sam", 56, 75000)); listEmployees.add(new Employee("Alex", 30, 120000)); listEmployees.add(new Employee("Peter", 25, 60000)); System.out.println("Before sorting: " + listEmployees); Collections.sort(listEmployees, new EmployeeAgeComparator()); System.out.println("After sorting: " + listEmployees);Output:
Before sorting: [(Tom, 45, 80000), (Sam, 56, 75000), (Alex, 30, 120000), (Peter, 25, 60000)] After sorting: [(Peter, 25, 60000), (Alex, 30, 120000), (Tom, 45, 80000), (Sam, 56, 75000)]See the detailed specification of the compare() method.