Sorting Multidimensional Arrays

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;
}

First, this technically isn’t a multi-dimension array. This is what is called a ‘jagged array’… an array of arrays.

Next, in your example code, you’re sorting the array BIGARRAY, doing so would just reorder array1 thru array8 around inside BIGARRAY.

You seem to want to sort the elements of array1 thru array8. So you need to go through and sort each of them.

This is a general C# programming question and not Unity specific. But anyways, the sorting you’re attempting to do will only work if your arrays are all of the same length, in which case an array of arrays wouldn’t be the best data structure to use in this case. You’re better off with an array of objects, e.g.:

public class Data
{
  public string d1;
  public string d2;
  .
  .
  .
  public string d8;
}

var dataLength = // found the length of your data here
var bigArray = new DecryptedData[dataLength];
// stuff bigArray with data here.

Array.Sort(bigArray, (a, b) => string.Compare(a.d2, b.d2));
// bigArray is now sorted according to d2.

If you must use an array of arrays, then you can look into the Array.Sort(Array, Array) method. But in all honesty, I think you need to spend more time getting familiar with the language. The array of arrays solutions should only occur to someone who doesn’t know about classes and structs, and those are pretty fundamental subjects that you ought to be familiar with already.

Hmm i see… well first thanks for the answers. I’d like to point out that the arrays have always the same length but i get the idea, this is not the best way to do it. So i think i’ll fiddle with the array of objects concept :slight_smile: btw yes i actually do know about them but just needed this idea to alter a leftover project… But nonetheless, i think i’ll write some of the scripts from scratch :slight_smile: Thanks anyways…