Java Sorting using Comparator
Lets see how we can sort our Employee objects using different criteria like id, name, salary.
SortTest remains the main class that will perform creating and filling objects for us. This time employee class is plain old java classs that holds some data about each employee we create.
Here I have created scenarios or you can say strategies OR classes for sorting as SortById, SortByName, SortBySalary. Each of above class implements comparator interface and overrides compare method, which takes two objects to compare and return eithre -1, 1 or 0.
Note: I have added few helper functions that really helps while coding like print – this takes array list of objects and prints in format I want instead of class id and all that. toString – overridden this function so I can return the class string as required
Here goes the 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
import java.util.*; public class SortTest { public static void main(String args[]) { new SortTest(); } public SortTest() { //fill some employee objects ArrayList list = new ArrayList(); list.add(new Employee(500,"Shifoo",150000)); list.add(new Employee(504,"Oogway",120000)); list.add(new Employee(503,"Tigress",100000)); list.add(new Employee(730,"Mantis",45000)); System.out.println("Initial List :"); print(list); System.out.println("n"); Collections.sort(list,new SortyById()); System.out.println("Sorted List By Id:"); print(list); System.out.println("n"); Collections.sort(list,new SortyByName()); System.out.println("Sorted List By Name:"); print(list); System.out.println("n"); Collections.sort(list,new SortyBySalary()); System.out.println("Sorted List By Salary:"); print(list); System.out.println("n"); } public void print(ArrayList list) { Iterator it = list.iterator(); while(it.hasNext()) { Employee emp = (Employee) it.next(); System.out.println(emp); } } } class Employee { public int id; public String name; public double salary; public Employee(int id, String name,double salary ) { this.id = id; this.name = name; this.salary = salary; } public String toString() { return this.id +", "+this.name+", "+this.salary; } } //scenario | strategy - I class SortyById implements Comparator { public int compare(Object object1, Object object2) { int value=0; Employee emp1 = (Employee) object1; Employee emp2 = (Employee) object2; if(emp1.id > emp2.id) value = 1; else if(emp1.id < emp2.id) value = -1; else if(emp1.id == emp2.id) value = 0; return value; } } //scenario | strategy - II class SortyByName implements Comparator { public int compare(Object object1, Object object2) { Employee emp1 = (Employee) object1; Employee emp2 = (Employee) object2; return emp1.name.compareTo(emp2.name); } } //scenario | strategy - III class SortyBySalary implements Comparator { public int compare(Object object1, Object object2) { int value=0; Employee emp1 = (Employee) object1; Employee emp2 = (Employee) object2; if(emp1.salary > emp2.salary) value = 1; else if(emp1.salary < emp2.salary) value = -1; else if(emp1.salary == emp2.salary) value = 0; return value; } } |
Output:
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 |
Initial List : 500, Shifoo, 150000.0 504, Oogway, 120000.0 503, Tigress, 100000.0 730, Mantis, 45000.0 Sorted List By Id: 500, Shifoo, 150000.0 503, Tigress, 100000.0 504, Oogway, 120000.0 730, Mantis, 45000.0 Sorted List By Name: 730, Mantis, 45000.0 504, Oogway, 120000.0 500, Shifoo, 150000.0 503, Tigress, 100000.0 Sorted List By Salary: 730, Mantis, 45000.0 503, Tigress, 100000.0 504, Oogway, 120000.0 500, Shifoo, 150000.0 |
As you can see –
- we can create multiple sort strategy for sorting
- we can compare heterogeneous objects
Nice example to separate two object comparing example
This really helping..!! Thank you
Nice post. The example code was very useful for clear understating.
Cool, I am happy it helped you.