Example class would be inherited by sort class and will implement comparator of type Example. Code would be like:
class sort extends Example implements Comparator<Example>{
public int compare(Example e1, Example e2){
if(e1.a > e2.a) return 1;
else if(e1.a < e2.a) return -1;
else return 0;
}
}
The given code will work for sorting array of Object type Example.
Actually it would be better to implement comparable from Example itself like:
class Example implements Comparable<Example>{
int a;
char b;
String c;
@Override
public int compareTo(Example e) {
if(this.a == e.a)
return 0;
else
return this.a > e.a ? 1 : -1;
}
}
//For calling sort
public static void main(String args[]){
ArrayList<Example> arr = new ArrayList<>();
Collections.sort(arr, new Example());
}
or
public static void main(String args[]){
Example[] arr = new Example[n];
Arrays.sort(arr, new Example());
}