0

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?

r3plica
  • 13,017
  • 23
  • 128
  • 290
  • 1
    Well you return -1 if it is designable.... – epascarello Oct 07 '15 at 14:27
  • 1
    That answer should help you: http://stackoverflow.com/questions/2784230/javascript-how-do-you-sort-an-array-on-multiple-columns – Buzinas Oct 07 '15 at 14:27
  • Think of it this way: you want to sort *first* by designability, and *then* (to break ties) by priority. – Touffy Oct 07 '15 at 14:29
  • Let's keep this in the original thread. If you need more help with the algorithm, leave me another comment on my answer. Having said this, I do not see what you mean. This code as is will produce exactly your desired outcome. – deceze Oct 07 '15 at 14:32
  • techincally this is a different question. The other question asked how to sort on two properties. You answered that (and I marked it). This question just needs to know how to reverse the order or the array because when I use your answer it gives me the exact opposite to what I wanted (although I didn't specify that in the first question, hence why I created this one) – r3plica Oct 07 '15 at 14:35
  • I'd agree with that ***if*** the premise of the question was at least sound. It's not. This code produces your desired outcome as far as I can tell. – deceze Oct 07 '15 at 14:36
  • try this http://jsfiddle.net/maio/oqqky2xr/ – maioman Oct 07 '15 at 14:42

1 Answers1

0

If the isDesignable differs, sort by them, if not, sort by priority:

var garments = [{
    name: 'garment 1',
    isDesignable: false,
    priority: 3
}, {
    name: 'garment 2',
    isDesignable: true,
    priority: 3
}, {
    name: 'garment 3',
    isDesignable: true,
    priority: 0
}];

var x = garments.sort(function(a, b) {
  if (a.isDesignable > b.isDesignable) return -1;
  if (a.isDesignable < b.isDesignable) return 1;
  if (a.priority < b.priority) return -1;
  if (a.priority > b.priority) return 1;
  return 0;
});

document.write('<pre><code>' + JSON.stringify(x, null, 2) + '</code></pre>');

Logic got from Javascript, how do you sort an array on multiple columns?

Community
  • 1
  • 1
Buzinas
  • 11,597
  • 2
  • 36
  • 58