Advertisement
ivorakitin

Comparator

May 2nd, 2024 (edited)
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. // Java Program to Demonstrate Working of
  2. // Comparator Interface
  3.  
  4. import java.lang.*;
  5. import java.util.*;
  6.  
  7.  
  8. public class Main {
  9.     public static void main(String[] args) {
  10.         ArrayList<Student> ar = new ArrayList<Student>();
  11.  
  12.         ar.add(new Student(111, "John", "London"));
  13.         ar.add(new Student(131, "Smith", "NYC"));
  14.         ar.add(new Student(121, "Stephan", "Berlin"));
  15.         ar.add(new Student(101, "Andrew", "London"));
  16.         ar.add(new Student(105, "Andrew", "Athens"));
  17.  
  18.         System.out.println("Unsorted:");
  19.  
  20.         for (int i = 0; i < ar.size(); i++)
  21.             System.out.println(ar.get(i));
  22.  
  23.         Collections.sort(ar, new SortByRoll(true));
  24.  
  25.         System.out.println("\nSorted by rollno:");
  26.  
  27.         for (int i = 0; i < ar.size(); i++)
  28.             System.out.println(ar.get(i));
  29.  
  30.         Collections.sort(ar, new SortByName(true));
  31.  
  32.         System.out.println("\nSorted by name:");
  33.  
  34.         for (int i = 0; i < ar.size(); i++)
  35.             System.out.println(ar.get(i));
  36.     }
  37. }
  38.  
  39. class Student {
  40.     private int rollno;
  41.     private String name, address;
  42.  
  43.     public Student(int rollno, String name, String address) {
  44.         this.rollno = rollno;
  45.         this.name = name;
  46.         this.address = address;
  47.     }
  48.     public int getRollno() {
  49.         return rollno;
  50.     }
  51.  
  52.     public String getName() {
  53.         return name;
  54.     }
  55.  
  56.     public String getAddress() {
  57.         return address;
  58.     }
  59.  
  60.     public String toString() {
  61.         return this.rollno + " " + this.name + " " + this.address;
  62.     }
  63. }
  64.  
  65. class SortByRoll implements Comparator<Student> {
  66.  
  67.     private boolean ascOrder;
  68.     public SortByRoll(boolean order) {
  69.         this.ascOrder = order;
  70.     }
  71.     // Sorting in ascending or descending order of roll number
  72.     //ascOrder = true
  73.     //ascOrder = false
  74.     public int compare(Student a, Student b) {
  75.         if (ascOrder)
  76.             return a.getRollno() - b.getRollno();
  77.         else
  78.             return b.getRollno() - a.getRollno();
  79.     }
  80. }
  81.  
  82.  
  83. class SortByName implements Comparator<Student> {
  84.     private boolean ascOrder;
  85.     public SortByName(boolean order) {
  86.         this.ascOrder = order;
  87.     }
  88.     // Sorting in ascending or descending order of roll number
  89.     //ascOrder = true
  90.     //ascOrder = false
  91.     public int compare(Student a, Student b) {
  92.         if (ascOrder) {
  93.             if (a.getName().compareTo(b.getName()) == 0) {
  94.                 if (ascOrder) {
  95.                     return a.getAddress().compareTo(b.getAddress());
  96.                 }
  97.                 else {
  98.                     return b.getAddress().compareTo(a.getAddress());
  99.                 }
  100.             }
  101.             else return a.getName().compareTo(b.getName());
  102.         }
  103.         else {
  104.             if (b.getName().compareTo(a.getName()) == 0) {
  105.                 if (ascOrder) {
  106.                     return a.getAddress().compareTo(b.getAddress());
  107.                 } else {
  108.                     return b.getAddress().compareTo(a.getAddress());
  109.                 }
  110.             }
  111.             else return b.getName().compareTo(a.getName());
  112.         }
  113.     }
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement