Monday 15 July 2013

JavaScript: Sorting with exception -


Let's say I have an array of objects (for the purpose of simple display I will show it as just an array )

I need to sort alphabetically (though, easy part), however, I need to sort it in a way that a certain word, Say that NEW will end in the very end.

['alpha', 'test \' /

is the function that I sort of

  var sorted keywords = Function (a, b) {back to a.word> Bword? 1 1; };   

So, I get sorted word arrays, and then again make another array again and again press the word in the new array until the word is equal to NEW . In that case I am setting it apart and attach it to the very end of this new array before returning it. There has been a better and more efficient way of doing this.

Thank you. new

new before Change your comparator to see:

  function (A, B) {if ((a.word === 'NEW')! = (B.word === 'NEW') ) {Return a.word === 'NEW'? 1 1; } Back to a.word & gt; Bword? 1: a.word & lt; Bword? -1: 0; }    

No comments:

Post a Comment