I already tried:
let age = arr.sort(function(a, b) {
return b -a;
});
it was good for simple array, but it does not work. In this array:
let list = [
[
['firstName', 'Joe'],
['age', 42],
['gender', 'male'],
],
[
['firstName', 'Mary'],
['lastName', 'Jenkins'],
['age', 36],
['gender', 'female'],
],
[
['lastName', 'Kim'],
['age', 40],
['gender', 'female'],
],
];
it's double array, and 'age' index locate is different. How can I approach the 'age' and align? I want to sort ascending.
I already tried:
let age = arr.sort(function(a, b) {
return b -a;
});
it was good for simple array, but it does not work. In this array:
let list = [
[
['firstName', 'Joe'],
['age', 42],
['gender', 'male'],
],
[
['firstName', 'Mary'],
['lastName', 'Jenkins'],
['age', 36],
['gender', 'female'],
],
[
['lastName', 'Kim'],
['age', 40],
['gender', 'female'],
],
];
it's double array, and 'age' index locate is different. How can I approach the 'age' and align? I want to sort ascending.
Share Improve this question edited Jan 16, 2021 at 11:13 Mark Rotteveel 109k227 gold badges156 silver badges220 bronze badges asked Jan 5, 2021 at 6:17 LooliiLoolii 4555 silver badges13 bronze badges 04 Answers
Reset to default 17You can use fromEntries
to convert your array item into object and then sort. But you should consider updating the item to object to avoid this unnecessary conversion.
const list = [ [ ['firstName', 'Joe'], ['age', 42], ['gender', 'male'], ], [ ['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['gender', 'female'], ], [ ['lastName', 'Kim'], ['age', 40], ['gender', 'female'], ], ];
list.sort((a, b) => Object.fromEntries(a).age - Object.fromEntries(b).age);
console.log(list);
You can do the following by finding the index of the child array where we can find age. From the example we can see that age is at index 0 and the age value is at index 1 of the sub array. You can than pare the age values.
list = [
[
['firstName', 'Joe'],
['age', 42],
['gender', 'male'],
],
[
['firstName', 'Mary'],
['lastName', 'Jenkins'],
['age', 36],
['gender', 'female'],
],
[
['lastName', 'Kim'],
['age', 40],
['gender', 'female'],
],
];
res = list.sort((a, b) => {
ageIndexA = a.findIndex(item => item[0] ==='age');
ageIndexB = b.findIndex(item => item[0] === 'age');
return a[ageIndexA][1] - b[ageIndexB][1];
});
console.log(res);
The following should work:
let list = [
[
['firstName', 'Joe'],
['age', 42],
['gender', 'male'],
],
[
['firstName', 'Mary'],
['lastName', 'Jenkins'],
['age', 36],
['gender', 'female'],
],
[
['lastName', 'Kim'],
['age', 40],
['gender', 'female'],
],
];
const pareFunction = (a,b) => {
return a.age - b.age;
}
const sortList = (arr) => {
const objectList = arr.map(e => Object.fromEntries(e));
return objectList.sort(pareFunction);
}
console.log('sorted:', sortList(list));
The following will sort your array according to ascending age:
let list = [
[
['firstName', 'Joe'],
['age', 42],
['gender', 'male'],
],
[
['firstName', 'Mary'],
['lastName', 'Jenkins'],
['age', 36],
['gender', 'female'],
],
[
['lastName', 'Kim'],
['age', 40],
['gender', 'female'],
],
];
let age = list.sort(function(a, b) {
return a.filter(aa=>aa[0]=="age")[0][1]
-b.filter(bb=>bb[0]=="age")[0][1];})
console.log(age)
// this is a better approach:
let listo=list.map(e=>e.reduce((a,[k,v])=>(a[k]=v,a), {}));
console.log(listo.sort((a,b)=>a.age-b.age))
However, it would be better to store your data in a different format:. My second part generates such a format in listo
. The sorting function is then trivial again ((a,b)=>a.age-b.age
):
[
{
"firstName": "Mary",
"lastName": "Jenkins",
"age": 36,
"gender": "female"
},
{
"lastName": "Kim",
"age": 40,
"gender": "female"
},
{
"firstName": "Joe",
"age": 42,
"gender": "male"
}
]
``´