I have a variable and array of objects e.g:
var selectedName = 'fff';
[{
percentage: Math.round(percentage),
count: count,
name: 'bbb',
index: 11,
},
{
percentage: Math.round(percentage),
count: 200,
name: 'aaa',
index: 2,
},
{
percentage: Math.round(percentage),
count: 400,
name: 'All',
index: 7,
},
{
percentage: Math.round(percentage),
count: count,
name: 'fff',
index: 8,
},
{
percentage: Math.round(percentage),
count: count,
name: 'ccc',
index: 3,
}],
I want to sort these as follows: the object which has the name 'All' should always be first. The next object should be the one with the name that matches selectedName, in this case 'fff'. And then the rest of the objects should be ordered by ascending order of their 'index' property.
Is this possible in one Array.sort() method?
arr.sort(compareFunction)- Look under the Description heading on that page you linked to for examples ofcompareFunction.