I need help with sorting 2D array. I\'ve got array with two rows
[5, 3, 4, 1, 2]
[10,20,30,40,50]
and I need to sort it to look like this:
You can use Comparator to do that. This thread has some useful information for C++ . Sample code in Java is like,
Arrays.sort(a, new Comparator() {
@Override
public int compare(Long[] o1, Long[] o2) {
Long t1 = o1[1];
Long p1 = o1[0];
Long t2 = o2[1];
Long p2 = o2[0];
if (t1 == t2) {
return (p1 > p2 ? 1 : (p1 == p2 ? 0 : -1));
} else {
return (t1 < t2 ? -1 : 1);
}
}
});
Do your comparison login in compare
method and return the relevant values to sort the array accordingly.