one of the most important question for interviews of java programmer is how to sort list of objects in java ,lets understand the solution using scenario of Patients , we will create a class Patient with two properties patientId and patientName and then we will provide options to sort list of patients by patientId and PatientName.
Step 1 : create Patient.java class as below :
package com.sorting.sv; public class Patient { private int patientId; private String patientName; public Patient() { } public Patient(int patientId, String patientName) { this.patientId = patientId; this.patientName = patientName; } public int getPatientId() { return patientId; } public void setPatientId(int patientId) { this.patientId = patientId; } public String getPatientName() { return patientName; } public void setPatientName(String patientName) { this.patientName = patientName; } @Override public String toString() { return "Patient [patientId=" + patientId + ", patientName=" + patientName + "]"; } }
Step 2: Now In patient class we have two variables , patientId and patientName as we want to sort by both ,lets create sorter classes by implementing Comparator interface. for sorting by patientId , create IdSorter.java class as below :
package com.sorting.sv; import java.util.Comparator; public class IdSorter implements Comparator<Patient> { @Override public int compare(Patient p1, Patient p2) { if(p1.getPatientId()>p1.getPatientId()) { return 1; } else if(p1.getPatientId()<p2.getPatientId()) { return -1; }else { return 0; } } }
for sorting patients by name create NameSorter.java class as below :
package com.sorting.sv; import java.util.Comparator; public class NameSorter implements Comparator{ @Override public int compare(Patient o1, Patient o2) { return o1.getPatientName().compareTo(o2.getPatientName()); } }
finally for testing write main class as below :
package com.sorting.sv; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Scanner; public class TestSorting { public static void main(String[] args) { ListmyPatientList = Arrays.asList(new Patient(3, "A"), new Patient(1, "C"), new Patient(2, "B")); Scanner scn = new Scanner(System.in); int option = 0; try { System.out.println("original list is :"); for(Patient patient:myPatientList) { System.out.println(patient); } do { System.out.println("Enter 1 for Sort by Id"); System.out.println("Enter 2 for Sort by Name"); System.out.println("Enter 0 for EXIT:"); option = scn.nextInt(); if (option == 1) { Collections.sort(myPatientList, new IdSorter()); System.out.println("List sorted by patientId"); for(Patient patient:myPatientList) { System.out.println(patient); } } else if (option == 2) { System.out.println("List sorted by patientName"); Collections.sort(myPatientList, new NameSorter()); for(Patient patient:myPatientList) { System.out.println(patient); } } else if(option == 0) { System.out.println("Exited successfuly"); } else { System.out.println("Please enter 1 or 2 or 3 :"); } }while(option!=0); } catch (Exception e) { e.printStackTrace(); } finally { scn.close(); } } }
Output :
original list is :
Patient [patientId=3, patientName=A]
Patient [patientId=1, patientName=C]
Patient [patientId=2, patientName=B]
Enter 1 for Sort by Id
Enter 2 for Sort by Name
Enter 0 for EXIT:
1
List sorted by patientId
Patient [patientId=1, patientName=C]
Patient [patientId=2, patientName=B]
Patient [patientId=3, patientName=A]
Enter 1 for Sort by Id
Enter 2 for Sort by Name
Enter 0 for EXIT:
2
List sorted by patientName
Patient [patientId=3, patientName=A]
Patient [patientId=2, patientName=B]
Patient [patientId=1, patientName=C]
Enter 1 for Sort by Id
Enter 2 for Sort by Name
Enter 0 for EXIT:
0
Exited successfuly
Thanks For Reading , Happy Learning.