4 Replies Latest reply: Apr 1, 2008 11:12 AM by 800328

# 2d array sorting....with double type values

if there is an array
``````double[][] Score = new double[3][2];
Score[0][0] = 2.34;
Score[0][1] = 2;

Score[1][0] = 4.56;
Score[1][1] = 7;

Score[2][0] = 3.45;
Score[2][1] = 1;``````
How can i sort the array in ascending order, according to the values in first column(Double)..
I have encountered the method called "Compare" but, it works only with int values...
So, any idea????

here is my code
``````import java.util.*;
class sort{

public static void main(String[] args) {
double[][] names = new double[3][2];
names[0][0] = 2.34;
names[0][1] = 2;

names[1][0] = 4.56;
names[1][1] = 7;

names[2][0] = 3.45;
names[2][1] = 1;
for(int i = 0; i < names.length; i++) {
System.out.println(names[0] + ", " + names[i][1]);
}

java.util.Arrays.sort(names, new java.util.Comparator<double[]>() {
public double compare(double[] o1, double[] o2) {
if((o1[0]) > (o2[0])) {
return 1;
} else if((o1[0]) < (o2[0])) {
return -1;
} else {
return 0;
}
}

}
);
System.out.println("after sorting");
for(int i = 0; i < names.length; i++) {

System.out.println(names[i][0] + ", " + names[i][1]);
}

}

}

and here are the errors i am gettingC:\Documents and Settings\Sumit\Desktop>javac sort.java
sort.java:20: <anonymous sort\$1> is not abstract and does not override abstract
method compare(double[],double[]) in java.util.Comparator
public double compare(double[] o1, double[] o2) {
^
1 error

``````
• ###### 1. Re: 2d array sorting....with double type values
compare() must return an int, not a double
• ###### 2. Re: 2d array sorting....with double type values
so, is there any other way to sort an array having values of type double......
• ###### 3. Re: 2d array sorting....with double type values
Yes, you've probably done it correctly even. But you have to make your compare method of type int, not double.
``public int compare(double[] o1, double[] o2)``
• ###### 4. Re: 2d array sorting....with double type values
thanks........newark....i got it...thank you very much