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.
We can use ArrayList as the intermediate structure and add the elements into the ArrayList using the add () method. ArrayList is a data structure that allows us to dynamically add elements.
There is no way to increase the alignment requirement for each array element, as the alignment requirement is for the whole array: This alignas (64) specifier produces the following result: There is a workaround. We can use an array of structures and align a filed in the structure or the structure itself: Both produces the desired result:
This tool is generally used to align walls, beams, and lines, but it can be used with other types of elements as well. The elements to align can be of the same type, or they can be from different families. You can align elements in a plan view (2D), 3D view, or elevation view. Align the ends of walls or beams with a selected beam, line, or wall.
The cursor displays with the align symbol. Select Multiple Alignment to align multiple elements with a selected element. (As an alternative, you can press Ctrl while selecting multiple elements to align.)
This tool is generally used to align walls, beams, and lines, but it can be used with other types of elements as well. The elements to align can be of the same type, or they can be from different families. You can align elements in a plan view (2D), 3D view, or elevation view.
You 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 compare 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 compareFunction = (a,b) => {
return a.age - b.age;
}
const sortList = (arr) => {
const objectList = arr.map(e => Object.fromEntries(e));
return objectList.sort(compareFunction);
}
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"
}
]
``´
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With