I have one array that holds multiple arrays that contain two numbers. I want to sort this array by ascending numbers.
This is how I'm creating the array of arrays. I start with two arrays, one holding the key and one holding the value. Then I join those two arrays into an object of key value pairs. The I convert that object into the new array of arrays.
keySliceArray.forEach((key, i) => result[key] = valueSliceArray[i])
console.log(result)
// This is the output of result
{
"73": 4028.4844548885576,
"81": 230.69935926538437,
"74.8": 3173.9828083733455,
"78.4": 1464.9795153429131,
"82.4": 0,
"77.2": 2034.647279686391,
"75.6": 2794.2042988110297,
"76.6": 2319.4811618581325,
"68.7": 6069.793943786011,
"69.3": 5784.960061614278,
"73.6": 3743.6505727168224,
"69.1": 5879.904689004858,
"74.5": 3316.399749459213,
"80.6": 420.58861404654635,
"80.1": 657.9501825229945,
"71.8": 4598.152219232035,
"76.5": 2366.9534755534196,
"80.4": 515.5332414371196,
"83.1": 0,
"83.8": 0,
"81.7": 0,
"83.3": 0,
"83.7": 0,
"71.1": 4930.458415099063,
"74.7": 3221.4551220686317,
"73.2": 3933.539827497977,
"75.7": 2746.7319851157354,
"78.8": 1275.0902605617584,
"77.4": 1939.7026522958095,
"78.6": 1370.034887952339
}
// This is how I'm converting the above object into an array of arrays.
var newArray = Object.keys(result).map(function (key) {
return [Number(key), result[key] ];
})
console.log(newArray)
This is the original output of the newArray variable
[
[
73,
4028.4844548885576
],
[
81,
230.69935926538437
],
[
74.8,
3173.9828083733455
],
[
78.4,
1464.9795153429131
],
[
82.4,
0
],
[
77.2,
2034.647279686391
],
[
75.6,
2794.2042988110297
],
[
76.6,
2319.4811618581325
],
[
68.7,
6069.793943786011
],
[
69.3,
5784.960061614278
],
[
73.6,
3743.6505727168224
],
[
69.1,
5879.904689004858
],
[
74.5,
3316.399749459213
],
[
80.6,
420.58861404654635
],
[
80.1,
657.9501825229945
],
[
71.8,
4598.152219232035
],
[
76.5,
2366.9534755534196
],
[
80.4,
515.5332414371196
],
[
83.1,
0
],
[
83.8,
0
],
[
81.7,
0
],
[
83.3,
0
],
[
83.7,
0
],
[
71.1,
4930.458415099063
],
[
74.7,
3221.4551220686317
],
[
73.2,
3933.539827497977
],
[
75.7,
2746.7319851157354
],
[
78.8,
1275.0902605617584
],
[
77.4,
1939.7026522958095
],
[
78.6,
1370.034887952339
]
]
I want it to look like this. I want sort the newArray variable in ascending order by the first number in each subsequent array.
[
[
68.7,
6069.793943786011
],
[
69.1,
5879.904689004858
],
[
69.3,
5784.960061614278
],
[
71.1,
4930.458415099063
],
[
71.8,
4598.152219232035
],
[
73,
4028.4844548885576
],
[
73.2,
3933.539827497977
],
[
73.6,
3743.6505727168224
],
[
74.5,
3316.399749459213
],
[
74.7,
3221.4551220686317
],
[
74.8,
3173.9828083733455
],
[
75.6,
2794.2042988110297
],
[
75.7,
2746.7319851157354
],
[
76.5,
2366.9534755534196
],
[
76.6,
2319.4811618581325
],
[
77.2,
2034.647279686391
],
[
77.4,
1939.7026522958095
],
[
78.4,
1464.9795153429131
],
[
78.6,
1370.034887952339
],
[
78.8,
1275.0902605617584
],
[
80.1,
657.9501825229945
],
[
80.4,
515.5332414371196
],
[
80.6,
420.58861404654635
],
[
81,
230.69935926538437
],
[
81.7,
0
],
[
82.4,
0
],
[
83.1,
0
],
[
83.3,
0
],
[
83.7,
0
]
[
83.8,
0
]
]
I have tried using the sort method like this but for some reason this just returns the original array. Any advice on how to achieve this is greatly appreciated!
let sortArray = newArray.sort(function (a, b) {
return a - b
})
CodePudding user response:
Your sort isn't working because you need to sort by the key of the nested object, not the nested object itself. Since your array contains tuples and you want to sort by the value of the first element in the tuple, this should work:
newArray.sort(function (a, b) {
return a[0] - b[0];
});
Also, note that Array.prototype.sort
sorts the array in place.
const result = {
"73": 4028.4844548885576,
"81": 230.69935926538437,
"74.8": 3173.9828083733455,
"78.4": 1464.9795153429131,
"82.4": 0,
"77.2": 2034.647279686391,
"75.6": 2794.2042988110297,
"76.6": 2319.4811618581325,
"68.7": 6069.793943786011,
"69.3": 5784.960061614278,
"73.6": 3743.6505727168224,
"69.1": 5879.904689004858,
"74.5": 3316.399749459213,
"80.6": 420.58861404654635,
"80.1": 657.9501825229945,
"71.8": 4598.152219232035,
"76.5": 2366.9534755534196,
"80.4": 515.5332414371196,
"83.1": 0,
"83.8": 0,
"81.7": 0,
"83.3": 0,
"83.7": 0,
"71.1": 4930.458415099063,
"74.7": 3221.4551220686317,
"73.2": 3933.539827497977,
"75.7": 2746.7319851157354,
"78.8": 1275.0902605617584,
"77.4": 1939.7026522958095,
"78.6": 1370.034887952339
}
var newArray = Object.keys(result).map(function (key) {
return [Number(key), result[key] ];
});
newArray.sort(function (a, b) {
return a[0] - b[0];
});
console.log(newArray);