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 :)
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;
}
Answer by Soraphis · Dec 17, 2015 at 08:48 PM
do i get you correct: you want to sort array2 alphabetically, and every other array according to this?
if so: https://jsfiddle.net/oopqmzv8/
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[i], (a, b) => String.Compare(table[column][Array.IndexOf(table[i], a)],
table[column][Array.IndexOf(table[i], b)], StringComparison.Ordinal));
}
}
its basicly the same as my fiddle but it should be faster and less to write
Wow thanks for the immediate answer :) Yes you are correct in what i need but unfortunately the link only points to an empty jsfiddle page :/ Perhaps you just missed it? :)
ahh your right :D i did not clicked on Save XD link is now updated: https://jsfiddle.net/oopqmzv8/
Hmm.. Well i sort of get what you're getting at and that looks pretty neat actually, thank you so much for trying to help :) But probably specific to Unity, i am getting and Index is Out of Range error.. Any idea why that might be?
Your answer

Follow this Question
Related Questions
Sorting an Array of Arrays for game Object 2 Answers
How to sort List of Lists by a certain Value (C#) 1 Answer
help please. I wrote a mergesort algorithm that does nothing. 0 Answers
How to make Unity singleton base class to support generics? 1 Answer
Help to Add some Sort and Binary Search in the code. 1 Answer