I don't know anything about Unity, but here is something that might help. It actually sorts arrays of objects by property but it can be used for your purposes as well.
You use it as:
objSort([array of arrays or objects],[numerical offset or property name],[optional--mode]);
mode, if used, determines the type of sort. The default is alphabetical ascending. You
can use: 'ad' --alphabetical descending, 'na' --numerical ascending, or 'nd' --numerical descending.
It does not change the original array. It returns an array with the objects/arrays sorted as requested.
The function:
function objSort(arr, prop, mode){
var key, i, len, all, ret, val;
key = {};
all = [];
ret = [];
len = arr.length;
for(i = 0; i < len; i++){
val = arr[i][prop];
if(!key[val]){
key[val] = [];
all.push(val);
}
key[val].push(arr[i]);
}
switch (mode){
case "na":
all.sort(function(a, b){return a-b});
break;
case "nd":
all.sort(function(a, b){return b-a});
break;
case "ad":
all.sort();
all.reverse();
break;
default:
all.sort();
}
len = all.length;
for(i = 0; i < len; i++){
ret = ret.concat(key[all[i]]);
}
return ret;
}
Note also that it allows you to do "layered" sorts. Start out with the least significant field and do the most significant last.
For example, lets say that you wish to have the following array sorted by last name, first name, and year of birth:
var k =[ ["John","Smith", 1975],
["Albert", "Smith", 1981],
["Betty", "Smith", 1969],
["Carl", "Adams", 1972],
["John","Smith", 1971],
["Adam","Doe", 1983]
];
k = objSort(k, 2, 'na'); //sort by year of birth
k = objSort(k ,0); //sort by first name
k = objSort(k,1); //sort by last name
console.log(k);
//this yields:
[ [ 'Carl', 'Adams', 1972 ],
[ 'Adam', 'Doe', 1983 ],
[ 'Albert', 'Smith', 1981 ],
[ 'Betty', 'Smith', 1969 ],
[ 'John', 'Smith', 1971 ],
[ 'John', 'Smith', 1975 ] ]