I am writing some java code for my project and I came across this issue that I do not know how to deal with. I have a two-dimensional array like this:
Object[][] array = new Object[10][3];
Which has inner types: [String, String, Double]
Example:
[[apple, car, 10.7], [melon, train, 3.4], [peach, plane, 5.2]]
I need to sort it by the last double, in the ascending order without creating my own sort function.
Desired result:
[[melon, train, 3.4], [peach, plane, 5.2], [apple, car, 10.7]]
Can someone please help me?
PS (I tried all of the solutions from the post that is tagged here as "this question was answered before" and none of those worked for me. The first reply here, however, solved my issue. Thank you!)
Arrays.sort(array, Comparator.comparingDouble(a -> (double) a[2]));