Another way to deal with this is to restructure the array for sorting purposes, and then once sorted to restructure again back to the original array.
Because we’ll want to check further info when an identical match occurs, let’s rework the sort function so that instead of checking one number against another, it instead accepts the whole row and checks the last items. If it comes across identical values it can then check the next from last, so so forth.
Let us sort these two arrays, so that when sorting according to the last column at 4, the bottom array ends up on the top due to the values in the first column.
[9, 5, 0, 3]
[5, 5, 0, 3]
The first array goes in to our sorting function as arrayA, and the second array goes in as arrayB.
Our sorting function compares the last value of each array, and when they’re the same it then steps left to the next-from-last and compares those. Whenever it comes across an identical match, it steps left and compares again.
That is how our sorting algorithm must work.
In order for our sorting algorithm to work like that, the array that is being sorted must be accessed as [row][col].
If the array that you want to sort is [col][row] then you must transpose it to a new array as [row][col], sort that, then transpose the sorted array back to the original array as [col][row]
Because your matrix is [col][row] you need to perform those extra transposition steps, since it’s not feasible for the sorting technique to work when they are the other way around.
Let’s start with creating an array using [col][row] ordering, so that we can show the result.
function show(array) {
var col, row;
for (row = 0; row < array[0].length; row += 1) {
for (col = 0; col < array.length; col += 1) {
document.write('[' + col + '][' + row + ']' + ' ' + array[col][row] + ' ');
}
document.write('<br>');
}
}
var myArray = [];
for (col = 0; col < 4; col += 1) {
myArray[col] = [];
for (row = 0; row < 7; row += 1) {
myArray[col][row] = parseInt(Math.random() * 10, 10);
}
}
document.write('Before:<br>');
show(myArray);
document.write('<br>');
Our customSort function needs to transpose the array, sort it, then return a retransposed version of the sorted array.
function customSort(array) {
var transposed = transpose(array);
transposed.sort(byLastUniqueItem);
return transpose(transposed);
}
myArray = customSort(myArray);
Here’s the transpose function:
function transpose(array) {
// Transposes a two-dimensional array so that
// array[col][row] is returned as array[row][col]
var transposed = [],
row,
col;
for (row = 0; row < array[0].length; row += 1) {
transposed[row] = [];
for (col = 0; col < array.length; col += 1) {
transposed[row][col] = array[col][row];
}
}
return transposed;
}
And here’s the byLastUniqueItem function, which allows us to walk backwards through each array if there’s an identical match:
function byLastUniqueItem(arrayA, arrayB) {
var index = arrayA.length,
a,
b,
direction = 0;
while (direction === 0 && index >= 0) {
index -= 1;
a = arrayA[index];
b = arrayB[index];
direction = (a == b ? 0 : (a < b ? -1 : 1));
}
return direction;
}
Here’s how it works where even nearly all items in a row are the same:
Before:
[0][0] 6 [1][0] 4 [2][0] 6 [3][0] 2
[0][1] 9 [1][1] 8 [2][1] 5 [3][1] 0
[0][2] 8 [1][2] 5 [2][2] 0 [3][2] 8
[0][3] 0 [1][3] 6 [2][3] 3 [3][3] 4
[0][4] 9 [1][4] 5 [2][4] 0 [3][4] 3
[0][5] 4 [1][5] 7 [2][5] 3 [3][5] 6
[0][6] 5 [1][6] 5 [2][6] 0 [3][6] 3
After:
[0][0] 9 [1][0] 8 [2][0] 5 [3][0] 0
[0][1] 6 [1][1] 4 [2][1] 6 [3][1] 2
[0][2] 5 [1][2] 5 [2][2] 0 [3][2] 3
[0][3] 9 [1][3] 5 [2][3] 0 [3][3] 3
[0][4] 0 [1][4] 6 [2][4] 3 [3][4] 4
[0][5] 4 [1][5] 7 [2][5] 3 [3][5] 6
[0][6] 8 [1][6] 5 [2][6] 0 [3][6] 8
And here’s the full test code:
function show(array) {
var col, row;
for (row = 0; row < array[0].length; row += 1) {
for (col = 0; col < array.length; col += 1) {
document.write('[' + col + '][' + row + ']' + ' ' + array[col][row] + ' ');
}
document.write('<br>');
}
}
function transpose(array) {
// Transposes a two-dimensional array so that
// array[col][row] is returned as array[row][col]
var transposed = [],
row,
col;
for (row = 0; row < array[0].length; row += 1) {
transposed[row] = [];
for (col = 0; col < array.length; col += 1) {
transposed[row][col] = array[col][row];
}
}
return transposed;
}
function byLastUniqueItem(arrayA, arrayB) {
var index = arrayA.length,
a,
b,
direction = 0;
while (direction === 0 && index >= 0) {
index -= 1;
a = arrayA[index];
b = arrayB[index];
direction = (a == b ? 0 : (a < b ? -1 : 1));
}
return direction;
}
function customSort(array) {
var transposed = transpose(array);
transposed.sort(byLastUniqueItem);
return transpose(transposed);
}
var myArray = [];
for (col = 0; col < 4; col += 1) {
myArray[col] = [];
for (row = 0; row < 7; row += 1) {
myArray[col][row] = parseInt(Math.random() * 10, 10);
}
}
document.write('Before:<br>');
show(myArray);
document.write('<br>');
myArray = customSort(myArray);
document.write('After:<br>');
show(myArray);