J
Jenny
I tried the code below. It did not work. I even created an Object[]
to hold Integers 1,2 and 3, it still did not work. Could you help me
to sort 1,2,3 to be 3,2,1? I know how to write code to sort it. I'd
like to learn the Comparator. Thanks a lot.
Comparator myComp = new Comparator(){
public int compare(Object o1, Object o2){
int[] a1 = (int[])o1;
int[] a2 = (int[])o2;
if(a1[0] < a2[0]){
return 1;
}else if(a1[0] > a2[0]){
return -1;
}else{
return 0;
}
}
};
int ans[] = {1,2,3};
Arrays.sort(ans,myComp);
to hold Integers 1,2 and 3, it still did not work. Could you help me
to sort 1,2,3 to be 3,2,1? I know how to write code to sort it. I'd
like to learn the Comparator. Thanks a lot.
Comparator myComp = new Comparator(){
public int compare(Object o1, Object o2){
int[] a1 = (int[])o1;
int[] a2 = (int[])o2;
if(a1[0] < a2[0]){
return 1;
}else if(a1[0] > a2[0]){
return -1;
}else{
return 0;
}
}
};
int ans[] = {1,2,3};
Arrays.sort(ans,myComp);