Sorting this array based on the attributes age and user has become my current challenge. The priority is to first sort by age, followed by sorting by user. In cases where the ages are the same, the sorting should be done based on the user attribute.
var users = [
{ 'user': 'kurt', 'age': 1 },
{ 'user': 'barney', 'age': 11 },
{ 'user': 'fred', 'sge': 3 },
{ 'user': 'alex', 'age': 7 },
{ 'user': 'box', 'age': 7 },
{ 'user': 'foo', 'age': 7 }
];
_.sortBy(users, (o) => [o.age, o.user]);
Output:
[
{ 'user': 'kurt', 'age': 1 },
{ 'user': 'barney', 'age': 11 },
{ 'user': 'fred', 'age': 3 },
{ 'user': 'alex', 'age': 7 },
{ 'user': 'box', 'age': 7 },
{ 'user': 'foo', 'age': 7 }
]
In the resulting output, it's noticed that age: 11 is positioned after age: 1, whereas it should actually come at the end. Is there a mistake in the approach I am taking here, or could this be an issue with the lodash sortBy function? The expected output should be as shown below:
Expected output:
[
{ 'user': 'kurt', 'age': 1 },
{ 'user': 'fred', 'age': 3 },
{ 'user': 'alex', 'age': 7 },
{ 'user': 'box', 'age': 7 },
{ 'user': 'foo', 'age': 7 },
{ 'user': 'barney', 'age': 11 }
]