5

Lets say I have an array of objects (for simpler display purposes I will just show it as an array)

[ 'TEST', 'NEW', 'ALPHA', 'ZOO', 'WHATEVER' ]

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

[ 'ALPHA', 'TEST', 'WHATEVER', 'ZOO', 'NEW' ]

Here is the function that I sort with

var sortedWords = function(a, b) {
    return a.word > b.word ? 1 : -1 ;
};

So, I get the sortedWords array, and then go over it again, create a yet another array and push words into the new array, unless the word equals NEW. In that case I set it aside and append at the very end of this new array before returning it. There got to be a better and more efficient way of doing this.

Thank you.

3
  • 4
    before your return: if(a == 'youWord'){return 1;} Commented Jun 22, 2013 at 19:31
  • @BrianHannay Post this as an answer! Commented Jun 22, 2013 at 19:33
  • 1
    @BrianHannay Remember that if a and b are both the target word, the result must be 0. Commented Jun 22, 2013 at 19:34

4 Answers 4

7

Change your comparator to look for NEW first:

function (a, b) {
    if ((a.word === 'NEW') != (b.word === 'NEW')) {
        return a.word === 'NEW' ? 1 : -1;
    }
    return a.word > b.word ? 1 :
           a.word < b.word ? -1 : 0;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, Chris! This does exactly what I needed, but apparently I have to wait for 7 minutes before I can accept this as an answer :)
@solefald can you check this jsfiddle.net/blackjim/X6krQ ? Maybe I missed something.
@Antonis: yep. since you are sorting a flat array (i sort array of objects), you need to change a.word to a and b.word to b
@solefald this is not the code that was submitted though. The initial code didn't work.
@Antonis: In my original question? See the first line. I mentioned that for simple display purposes I will show it is a flat array. Chris's solution works great in my case, obviously with some adjustments to match my object element names and the words I match for.
4

Use sort with a custom compareFunction

var ar = [ 'ALPHA', 'WHATEVER', 'NEW', 'ZOO', 'TEST' ];

ar.sort(function(a, b){
    var wordToBeLast = 'NEW';  // set your word here
    
    if(a===wordToBeLast){
        return 1;
    } else if(b===wordToBeLast){
        return -1;
    } else {
        return a.localeCompare(b);
    }
});

1 Comment

return a > b; is incorrect: the compare function needs to return a number, not a boolean, and the number should be 0 if the two items are equal.
2

You could define your comparator function as follows:

var sortedWords = function(a, b) {
    if (a.word === b.word) {
        // a and b are considered equal, the order is okay
        return 0;   
    }

    if (a.word === "NEW") {
        // "NEW" is considered larger than every other value, the order is wrong
        return 1;
    }

    // Correct order if b equals "NEW", otherwise lexicographical comparison
    return b.word === "NEW" ? -1 : (a.word > b.word ? 1 : -1);
};

2 Comments

Can you explain how this is an answer and not a comment?
@Blender: I didn't paste my fiddle code at that point of time yet.
0

There is simpler way using lodash, which can be extended to a more complex scenario, while keeping the code simple.

const strings = [ 'TEST', 'NEW', 'ALPHA', 'ZOO', 'WHATEVER' ];

_.sortBy(strings, [
    (string) => string !== 'NEW',
    (string) => string
  ])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.