I have an array of objects like:
const arr = [
{
name: 'John',
age: 20,
},
{
name: 'Mark',
age: 30,
},
...
]
And I have a service invocation which has an order parameter which is an object with two properties: a field which is the field I want to sort my array of object by and an 'asc' which is a Boolean value for do I want the list in ascending or descending order.
const order = {
field: 'name',
asc: true,
}
I would have started something like this but it does not seem to be the solution
orderedList = list.sort((a, b) => {
if (order.asc) {
return a[order.field] - b[order.field];
} else {
return b[order.field] - a[order.field];
}
});