I have a set of garments that look like this:
var garments = [{
name: 'garment 1',
isDesignable: false,
priority: 3
}, {
name: 'garment 2',
isDesignable: true,
priority: 3
}, {
name: 'garment 3',
isDesignable: true,
priority: 0
}];
I need to sort these by priority only if the garment is designable. If the garment is not designable, the garments should appear at the bottom of the list.
I currently have this function (provided from another question I asked):
garments.sort(function (a, b) {
if (a.isDesignable == b.isDesignable) {
return a.priority - b.priority;
} else if (a.isDesignable) {
return -1;
} else {
return 1;
}
});
This works, but it appears to have sort the array in the wrong order. Non-designable items appear first in the array and higher priorities come next decreasing as you go along the array. What I need is the first item of the array to have the lowest priority (0 in this case) and the next item to be higher priority with the non designables at the end of the array so that the array will look like this:
var garments = [{
name: 'garment 3',
isDesignable: true,
priority: 0
}, {
name: 'garment 2',
isDesignable: true,
priority: 3
}, {
name: 'garment 1',
isDesignable: false,
priority: 3
}];
Can someone show me how to redo the sort function that it works as I need it?