I have an Array like this one:
var arrValues1 = [
[11, 58],
[18, 45],
[13, 23],
[15, 68],
[23, 32],
[45, 45],
[19, 68],
[88, 68]
];
In order to sort it by the index 1, I have used the following function:
sortIn(arr, prop) {
return arr.sort((a, b) => {
if (a[prop] > b[prop]) {
return 1;
} else if (a[prop] < b[prop]) {
return -1;
} else {
return 0;
}
});
}
arrValues2.push(sortIn(arrValues1, 1));
So, I get this result:
var arrValues2 = [
[13, 23],
[23, 32],
[45, 45],
[18, 45],
[11, 58],
[19, 68],
[88, 68],
[15, 68]
];
My problem is about the duplicate values (45 and 68 in this example). If I have duplicate values, I need to sort these considering the index 0 value. So, the final result would be:
var arrValues2 = [
[13, 23],
[23, 32],
[18, 45],// > these 2 cases were reordered
[45, 45],//
[11, 58],
[15, 68],// > these 3 cases were reordered
[19, 68],//
[88, 68] //
];
It's important keeping all the positions, changing only the duplicate values ordering. How can I achieve that?