I have a class (that implement Comparable) composed by:
double[1][2];
I would sorting my arrayList and i tried in this way but i reported an error:
public double compareTo(Matrix another) {
return this.getCell(0,1).compareTo(another.getCel(0,1));
}
How can sorting in this way?
Presumably, the call to getCell()
returns a double
, which doesn't have methods (like (compareTo()
).
Also, the compareTo()
method of the Compareae
interface must return int
, not double
.
Try this:
public int compareTo(Matrix another) {
double diff = getCell(0,1) - another.getCel(0,1);
return diff == 0 ? 0 : diff > 0 ? 1 : -1;
}
This solution will work in all java versions. For java 7+, you may do thus instead:
public int compareTo(Matrix another) {
return Double.compare(getCell(0, 1), another.getCell(0, 1));
}
Also note the slight clean up of removing the redundant this.
from your code.