Java Sort using Comparable or Comparator
In Java, it is straightforward to sort objects of the predefined class such as Integer, Double, Float and String. This is because these classes have been implemented the Comparable interface. Comparable defines a natural ordering, as when you’re defining it, you specify which one is considered “less than” or “greater than” the another.
How can you sort custom Objects? For example, suppose you have a list of objects of Student class, which has name, age, and score attributes. You may want to sort the students by their scores. Another example is to sort a list strings by their length.
In order to sort a list of custom Objects, we can use the Java Comparable or Comparator interface.
Implement Comparable interface
In the following example, we define a Student class, and implement the Comparable interface such that it can be sorted by the students’ score.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package learn.example; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Student implements Comparable<Student>{ String name; int age; double score; public Student(String name, int age, double score) { this.name = name; this.age = age; this.score = score; } @Override public int compareTo(Student o) { if ( this.score < o.score) { return -1; } if (this.score > o.score) { return 1; } return 0; } public String toString(){ return "Student Info: <name = " + name + ",age=" + age + ",score="+score +">"; } public static void main(String[] args) { List<Student> list = new ArrayList<>(); list.add(new Student("Lily", 23, 90)); list.add(new Student("david", 30, 93)); list.add(new Student("Mary", 25, 88)); System.out.println("before sort"); System.out.println(list); System.out.println("after sort"); Collections.sort(list); System.out.println(list); } } |
The output:
before sort
[Student Info: <name = Lily,age=23,score=90.0>, Student Info: <name = david,age=30,score=93.0>, Student Info: <name = Mary,age=25,score=88.0>]
after sort
[Student Info: <name = Mary,age=25,score=88.0>, Student Info: <name = Lily,age=23,score=90.0>, Student Info: <name = david,age=30,score=93.0>]
Sort Java objects using Comparator
There are many cases you have no control of the objects to be sorted. For instance, if the student class is defined by somebody else, and you want to sort the students by their age information. In this case, you can use Comparator. The following example shows how to call the sort method of Java List by providing an custom Comparator.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
list.sort(new Comparator<Student>() { // we sort the student in asc order of student ages @Override public int compare(Student o1, Student o2) { if(o1.age < o2.age) { return -1; } if(o1.age > o2.age) { return 1; } return 0; } }); System.out.println("after sort by age in asc order "); System.out.println(list); |
The output is:
after sort by age
[Student Info: <name = Lily,age=23,score=90.0>, Student Info: <name = Mary,age=25,score=88.0>, Student Info: <name = david,age=30,score=93.0>]
Sort Java Strings by length
Suppose you have a list of Strings, you want to sort them by their length. We can define a comparator to do this. See the following code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package learn.example; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class SortString { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("hello"); list.add("Li"); list.add("learn4master"); list.add("lean java"); list.add("I like java"); System.out.println("without sort"); System.out.println(list); System.out.println("sort in nature order"); Collections.sort(list); System.out.println(list); System.out.println("sort by length"); Collections.sort(list, new Comparator<String>() { @Override public int compare(String o1, String o2) { Integer len1 = o1.length(); Integer len2 = o2.length(); return len1.compareTo(len2); } }); System.out.println(list); } } |
The output:
without sort
[hello, Li, learn4master, lean java, I like java]
sort in nature order
[I like java, Li, hello, lean java, learn4master]
sort by length
[Li, hello, lean java, I like java, learn4master]
http://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html
http://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html











