I have a multidimentional array
array = [];
array[0] = [1,3,8,4,9,5];
array[1] = [5,9,4,2,9,3,0];
array[2] = [5,2,6,1,3,8,4,9,5,17,2,9,3,0];
array[3] = [-1,0,20,6];
I want to sort it to get this result
[
[-1,0,0,0,1,1],
[2,2,2,3,3,3,3],
[4,4,4,5,5,5,5,6,6,8,8,9,9,9],
[9,9,17,20]
]
I have defined this function that helped me get this result
Array.prototype.sort_multi = function()
{
var arr = [];
this.forEach(function(element_arr,index_arr,array_arr)
{
element_arr.forEach(function(element,index,array)
{
arr.push(element);
});
});
arr.sort(function (a, b) { return a - b;});
this.forEach(function(element_arr,index_arr,array_arr)
{
element_arr.forEach(function(element,index,array)
{
array_arr[index_arr][index] = arr.shift();
});
});
return this;
}
My question is: is there a simple way to do this ? for example using the function sort, or a simple algorithm ... ?