- Given an array of user defined objects/POJO in java.
- We would like to sort user defined objects on certain properties/data members.
- We will use Arrays.sort method to sort objects.
1. Example – Sort POJO or user defined object using comparator interface.
Given a Array containing Vehicle objects. Vehicle class has following data members.
- public String model
- public String owner
- public String registerCity
- public LocalDate serviceDate
We would like to Sort Vehicle objects by model, owner and serviceDate. We will have a specialized Comparators to sort objects by model, owner & serviceDate. We will have following Comparators:
- Comparator to sort Vehicle objects by model
- Comparator to sort Vehicle objects by owner
- Comparator to sort Vehicle objects by serviceDate
2. Program – sort array of user defined objects/POJO (Java/Example)
package org.learn.collection.array; import java.time.LocalDate; import java.time.Month; import java.util.Arrays; import java.util.Comparator; class Vehicle { public String model; public String owner; public String registerCity; public LocalDate serviceDate; public Vehicle(String model, String owner, String registerCity, LocalDate serviceDate) { this.model = model; this.serviceDate = serviceDate; this.owner = owner; this.registerCity = registerCity; } public String toString() { return String.format("\n[Model = %s,Owner = %s,registeredCity = %s,ServiceDate = %s]", model, owner, registerCity, serviceDate.toString()); } } class SortByModel implements Comparator<Vehicle> { @Override public int compare(Vehicle vehicle1, Vehicle vehicle2) { return vehicle1.model.compareTo(vehicle2.model); } } class SortByOwner implements Comparator<Vehicle> { @Override public int compare(Vehicle vehicle1, Vehicle vehicle2) { return vehicle1.owner.compareTo(vehicle2.owner); } } class SortByServiceDate implements Comparator<Vehicle> { @Override public int compare(Vehicle vehicle1, Vehicle vehicle2) { return vehicle1.serviceDate.compareTo(vehicle2.serviceDate); } } public class ArraySortObject { public static void main(String[] args) { Vehicle[] arVehicle = { new Vehicle("S-Class", "Guzan", "NewYork", LocalDate.of(2014, Month.AUGUST, 01)), new Vehicle("C-Class", "Bedoya", "California", LocalDate.of(2009, Month.DECEMBER, 05)), new Vehicle("A-Class", "Cameron", "Illinois", LocalDate.of(2014, Month.JULY, 25)), new Vehicle("SL-Class", "Johnson", "Kansas", LocalDate.of(2011, Month.DECEMBER, 10)) }; //Print Unsorted array in java System.out.println("1. Unsorted Array of vehicle objects:"); System.out.println(Arrays.toString(arVehicle)); //Sort Vehicle objects by model Arrays.sort(arVehicle,new SortByModel()); System.out.println("2. Sorted Vehicles by Model number:"); System.out.println(Arrays.toString(arVehicle)); //Sort Vehicle objects by Owner Arrays.sort(arVehicle,new SortByOwner()); System.out.println("3. Sort Vehicles by Owner :"); System.out.println(Arrays.toString(arVehicle)); //Sort Vehicle objects by ServiceDate Arrays.sort(arVehicle,new SortByServiceDate()); System.out.println("4. Sorted Vehicle by Service date:"); System.out.println(Arrays.toString(arVehicle)); } }
3. Output – sort array of user defined objects/POJO (Java/Example)
1. Unsorted Array of vehicle objects: [ [Model = S-Class,Owner = Guzan,registeredCity = NewYork,ServiceDate = 2014-08-01], [Model = C-Class,Owner = Bedoya,registeredCity = California,ServiceDate = 2009-12-05], [Model = A-Class,Owner = Cameron,registeredCity = Illinois,ServiceDate = 2014-07-25], [Model = SL-Class,Owner = Johnson,registeredCity = Kansas,ServiceDate = 2011-12-10]] 2. Sorted Vehicles by Model number: [ [Model = A-Class,Owner = Cameron,registeredCity = Illinois,ServiceDate = 2014-07-25], [Model = C-Class,Owner = Bedoya,registeredCity = California,ServiceDate = 2009-12-05], [Model = S-Class,Owner = Guzan,registeredCity = NewYork,ServiceDate = 2014-08-01], [Model = SL-Class,Owner = Johnson,registeredCity = Kansas,ServiceDate = 2011-12-10]] 3. Sort Vehicles by Owner : [ [Model = C-Class,Owner = Bedoya,registeredCity = California,ServiceDate = 2009-12-05], [Model = A-Class,Owner = Cameron,registeredCity = Illinois,ServiceDate = 2014-07-25], [Model = S-Class,Owner = Guzan,registeredCity = NewYork,ServiceDate = 2014-08-01], [Model = SL-Class,Owner = Johnson,registeredCity = Kansas,ServiceDate = 2011-12-10]] 4. Sorted Vehicle by Service date: [ [Model = C-Class,Owner = Bedoya,registeredCity = California,ServiceDate = 2009-12-05], [Model = SL-Class,Owner = Johnson,registeredCity = Kansas,ServiceDate = 2011-12-10], [Model = A-Class,Owner = Cameron,registeredCity = Illinois,ServiceDate = 2014-07-25], [Model = S-Class,Owner = Guzan,registeredCity = NewYork,ServiceDate = 2014-08-01]]