In previous articles, we have discussed how to sort list of objects on the basis of single field using Comparable and Comparator interface
Read below articles for sorting ArrayList contents in both ascending and descending order on the basis of single field (i.e.; member variable)
But, what if we have a requirement to sort ArrayList of objects in accordance with 2 or 3 fields like
- 1st sort, according to customer name
- secondly sort according to customer city
- and 3rd sorting with respect to customer age
Steps to sort ArrayList contents w.r.t 3 member variables of Customer class:
- Define Customer class with 3 member variables namely customer name, city and age
- Write a custom sorting logic to sort Customer member variables wrt 3 fields as per sequence (Name –> City –> Age)
- Main test class, where we are going to create ArrayList and inserts some 10+ entries of Customer objects
- Finally printing Customer objects before & after custom sorting logic from main class
Customer.java
package in.bench.resources.customized.sorting.multiple.fields; public class Customer { // instance member variables String custName; String custCity; Integer custAge; // 3-arg parameterized constructor public Customer(String custName, String custCity, int custAge) { this.custName = custName; this.custCity = custCity; this.custAge = custAge; } // getter & setter public String getCustName() { return custName; } public void setCustName(String custName) { this.custName = custName; } public String getCustCity() { return custCity; } public void setCustCity(String custCity) { this.custCity = custCity; } public Integer getCustAge() { return custAge; } public void setCustAge(Integer custAge) { this.custAge = custAge; } // overriding toString() method @Override public String toString() { return "Customer [" + "custName=" + custName + ", custCity=" + custCity + ", custAge=" + custAge + "]"; } }
CustomerSortingComparator.java
package in.bench.resources.customized.sorting.multiple.fields; import java.util.Comparator; public class CustomerSortingComparator implements Comparator<Customer> { @Override public int compare(Customer cust1, Customer cust2) { // all comparison int compareName = cust1.getCustName() .compareTo(cust2.getCustName()); int compareCity = cust1.getCustCity() .compareTo(cust2.getCustCity()); int compareAge = cust1.getCustAge() .compareTo(cust2.getCustAge()); // 3-level comparison using if-else block if(compareName == 0) { return ((compareCity == 0) ? compareAge : compareCity); } else { return compareName; } } }
CustomerSortingTest.java
package in.bench.resources.customized.sorting.multiple.fields; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; public class CustomerSortingTest { public static void main(String[] args) { // create List & ArrayList reference to store customers List<Customer> listOfCustomers = new ArrayList<Customer>(); // create customer objects using constructor initialization Customer cust1 = new Customer("Shalini", "Chennai", 60); Customer cust2 = new Customer("Sneha", "Pune", 73); Customer cust3 = new Customer("Simran", "Bangalore", 37); Customer cust4 = new Customer("Trisha", "Hyderabad", 52); Customer cust5 = new Customer("Shalini", "Chennai", 70); Customer cust6 = new Customer("Abirami", "Bangalore", 48); Customer cust7 = new Customer("Trisha", "Mangalore", 45); Customer cust8 = new Customer("Sneha", "Pune", 62); Customer cust9 = new Customer("Shalini", "Chennai", 50); // add customer objects to ArrayList listOfCustomers.add(cust1); listOfCustomers.add(cust2); listOfCustomers.add(cust3); listOfCustomers.add(cust4); listOfCustomers.add(cust5); listOfCustomers.add(cust6); listOfCustomers.add(cust7); listOfCustomers.add(cust8); listOfCustomers.add(cust9); // before Sorting: iterate using Iterator & while-loop Iterator<Customer> custIterator = listOfCustomers.iterator(); System.out.println("Before Sorting: iterate using" + " Iterator & while-loop\n"); while(custIterator.hasNext()) { System.out.println(custIterator.next()); } // sorting using Collections.sort(al, comparator); Collections.sort(listOfCustomers, new CustomerSortingComparator()); // after Sorting: iterate using enhanced for-loop System.out.println("\n\nAfter Sorting: iterate using" + " enhanced for-loop\n"); for(Customer customer : listOfCustomers) { System.out.println(customer); } } }
Output:
Before Sorting: iterate using Iterator & while-loop Customer [custName=Shalini, custCity=Chennai, custAge=60] Customer [custName=Sneha, custCity=Pune, custAge=73] Customer [custName=Simran, custCity=Bangalore, custAge=37] Customer [custName=Trisha, custCity=Hyderabad, custAge=52] Customer [custName=Shalini, custCity=Chennai, custAge=70] Customer [custName=Abirami, custCity=Bangalore, custAge=48] Customer [custName=Trisha, custCity=Mangalore, custAge=45] Customer [custName=Sneha, custCity=Pune, custAge=62] Customer [custName=Shalini, custCity=Chennai, custAge=50] After Sorting: iterate using enhanced for-loop Customer [custName=Abirami, custCity=Bangalore, custAge=48] Customer [custName=Shalini, custCity=Chennai, custAge=50] Customer [custName=Shalini, custCity=Chennai, custAge=60] Customer [custName=Shalini, custCity=Chennai, custAge=70] Customer [custName=Simran, custCity=Bangalore, custAge=37] Customer [custName=Sneha, custCity=Pune, custAge=62] Customer [custName=Sneha, custCity=Pune, custAge=73] Customer [custName=Trisha, custCity=Hyderabad, custAge=52] Customer [custName=Trisha, custCity=Mangalore, custAge=45]
References:
- http://www.benchresources.net/java/collection-framework/
- http://www.benchresources.net/sorting-arraylist-using-comparable-and-comparator-in-java/
- http://www.benchresources.net/sorting-arraylist-in-descending-order-in-java/
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/set.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html
Happy Coding !!
Happy Learning !!