How do i sort multidimensional javascript array?

Hey there everyone. First, i know this has been answered before but only partly. So here goes my question, i’ll try to be as quick as possible :slight_smile:

Say i have 8 dynamically constructed string arrays using a way described below:

var array1 = decryptedData[0].ToString().Split(";"[0]); //decryptedData is a variable also filled dynamically
var array2 = decryptedData[1].ToString().Split(";"[0]);
var array3 = decryptedData[2].ToString().Split(";"[0]);
var array4 = decryptedData[3].ToString().Split(";"[0]);
var array5 = decryptedData[4].ToString().Split(";"[0]);
var array6 = decryptedData[5].ToString().Split(";"[0]);
var array7 = decryptedData[6].ToString().Split(";"[0]);
var array8 = decryptedData[7].ToString().Split(";"[0]);

Then, to be able to control all these data easliy and specifically to sort them properly like you would do in a data table; I create a multidimensional 2d array from them using this:

var BIGARRAY = [array1,array2,array3,array4,array5,array6,array7,array8];

So the question is; how do i sort all of them according to e.g. array2 ?
All arrays are strings but some of them are actually numbers as strings if that helps.

Being not even sure if that’s a correct syntax, tried this with failure;

System.Array.Sort(BIGARRAY, mySorting);
function mySorting(a,b) {
var str1 = a[0][0];
var str2 = b[0][0];
var n = str1.localeCompare(str2);
return n;
}

do i get you correct: you want to sort array2 alphabetically, and every other array according to this?

if so: Edit fiddle - JSFiddle - Code Playground

well its plain javascript but it should work to get the concept.

imagine the sub arrays as columns in a table, rearranging them so that the X’t (0-indexed) column is sorted:

first i change the 2d array so, that i have an array of rows (instead columns)
and then i sort them for the rows X’t entry

at last i change the array again, so that i get columns again and return it

Edit:

alternatively (note: its C# code)

 private void SortTable(string[][] table, int column) {
        // for each column in table:
        for (int i = 0; i < table.Length; i++) {
            Array.Sort(table_, (a, b) => String.Compare(table[column][Array.IndexOf(table*, a)],*_ 

table[column][Array.IndexOf(table*, b)], StringComparison.Ordinal));*
}
}
its basicly the same as my fiddle but it should be faster and less to write