Professional Documents
Culture Documents
Object Oriented Programming: Comparable & Comparator Interfaces
Object Oriented Programming: Comparable & Comparator Interfaces
Pilani Campus
……………………………………… ………………………………………
……………………………………… ………………………………………
……………………………………… ………………………………………
public int compareTo(Object other){ public int compareTo(Object other){
BOX box = (BOX) other; Student std = (Student) other;
......Logic for comparison …. ......Logic for comparison ….
} }
…………………………………. ………………………………….
} }
class Student{
private String name;
private String idNo;
StudentByName StudentByIdNo StudentByAge
private int age;
private String city;
…………………..
StudentByNameIdNo StudentByNameAge
…………………..
// Sort By Name
Comparator c1 = new StudentByName();
Arrays.sort(students,c1);
for(int i=0;i<students.length;i++)
System.out.println(students[i]);