Reputation: 2025
I'm having some troubles sorting my dynamic bidimensionnal array. I searched and found this solution :
Double[][] sortedOutput = new Double[length][4];
//...
// processing and filling my tab like that
sortedOutput[k][0] = content[i].getLeftSpeed);
sortedOutput[k][1] = content[i].getRightSpeed();
sortedOutput[k][2] = content[i].getNormAvgPowOutput();
// ...
// Now i'm trying to sort the tab
Arrays.sort(sortedOutput, new java.util.Comparator<double[]>()
{
public int compare(double[]a, double[]b)
{
return Double.compare(a[0], b[0]);
}
});
But somehow i doesn't work :
The method sort(T[], Comparator<? super T>) in the type Arrays is not applicable for the arguments (Double[][], new Comparator<double[]>(){})
Why can't i use Arrays.sort
with double
?
I also tried to make a class :
package IOControl;
import java.util.Comparator;
public class CompareDoubleArray implements Comparator<double[]>
{
private int column;
public CompareDoubleArray(int column)
{
this.column = column;
}
public int compare(double[] arg0, double[] arg1)
{
return Double.valueOf(arg0[column]).compareTo(Double.valueOf(arg1[column]));
}
}
How should i use my class ? What did i do wrong? Any help would be nice, thanks :)
Upvotes: 1
Views: 367
Reputation: 8473
Arrays.sort takes only single dimensional array
as argument.So you can't use multidimensional array
here
Try this code may help you.
for(Double[] d : sortedOutput){
Arrays.sort(d , new Comparator<Double>(){
@Override
public int compare(Double o1, Double o2) {
//implement logic here
}
});
}
Upvotes: 0
Reputation: 9538
You need to type your Comparator
on Double[]
. A double[]
is not the same thing as Double[]
. One is an array of primitives the other is an array of Double
objects which are wrappers for primitives.
Arrays.sort(sortedOutput, new java.util.Comparator<Double[]>()
{
public int compare(Double[]a, Double[]b)
{
return Double.compare(a[0], b[0]);
}
});
Upvotes: 2