What I’m trying to do is re-order the multidimensional array based on the lengths of each of its arrays, highest to lowest.
var array0 = [9, 7, 1]
var array1 = [2]
var array2 = [20, 3, 8, 11]
var array3 = [0, 6];
var multi = [array0, array1, array2, array3];
var multiLengths = [];
$(multi).each(function(i) { // for loop using jQuery
multiLengths.push(this.length);
});
// multiLengths: 3,1,4,2
function sortDesc(a,b) { //sort in descending order
return b - a;
}
multiLengths.sort(sortDesc);
// somehow re-order multi to be: [array2, array0, array3, array1]
So how would I reorder the ‘multi’ array based on the new sorted order of ‘multiLengths’? It’s difficult because the new order is created using the sort function, so I can’t really index the results of the sortedMulti array.
You can just use sort on
multidirectly: