My array isn't being sorted properly. Can someone let me know what I am doing wrong?

...
 sortArray = new Array ("hello", "Link to Google", "zFile", "aFile");

//sort array
        if (dir == "asc") { 
            sortArray.sort(function(a,b){return a - b}); 
        } else { 
            sortArray.sort(function(a,b){return b - a});
        }

        for(var i=0; i<sortArray.length; i++) { 
            console.log(sortArray[i]);
        }

the log is showing them in the same order as they were entered.

share|improve this question

66% accept rate
@Tim Cooper's answer is the only one you need. – Brian Driscoll Mar 29 '11 at 17:54
of course you can also do this handy little shortcut .......................................................... (sortArray.sort())[(dir === 'asc' ? 'reverse' : 'slice' )]() – qwertymk Mar 29 '11 at 18:27
@mVChr: DEMO – qwertymk Mar 29 '11 at 18:34
feedback

4 Answers

up vote 8 down vote accepted

You want to make a comparison in your sort, not a subtraction:

if (dir == "asc") {
    sortArray.sort(function(a, b) {
        return a.toLowerCase() > b.toLowerCase()
    });
} else {
    sortArray.sort(function(a, b) {
        return b.toLowerCase() > a.toLowerCase()
    });
}

I also used toLowerCase() so that 'Link to Google' is placed appropriately.

See example →

share|improve this answer
I accepted yours for the toLowerCase() add on...though everyone deserves a +1. – PruitIgoe Mar 29 '11 at 18:11
This answer does not seem to work in Chrome. The comparator function is supposed to return 0 if a and b are equal, a positive integer if a should be sorted to a higher index than b, and a negative integer if a should be sorted to a lower index than b. – Mike Sep 16 at 20:38
feedback

Your comparator functions returns NaN, since it receives two strings, and performs subtraction, an operation that isn't well-defined on strings.

What you should have is something more like:

function(a,b){
   return a>b? 1 : (a<b ? -1 : 0);
}

or you can use localeCompare:

function(a,b){
   return a.localeCompare(b);
}

Remember to treat case appropriately, e.g. "L" < "a" whilst "l" > "a"

share|improve this answer
feedback

The trouble is that "a - b" is treating the strings like numbers, which returns NaN. You will get the behavior you are looking for (assuming you are looking for case-sensitive sorts) if you replace your sorts with:

    if (dir == "asc") { 
        sortArray.sort(function(a,b){return a < b ? -1 : 1}); 
    } else { 
        sortArray.sort(function(a,b){return b < a ? -1 : 1});
    }
share|improve this answer
feedback

You're trying to sort by subtracting strings, to which you'll get NaN.

share|improve this answer
Thanks everyone, obviously I misread the docs on that one - : D – PruitIgoe Mar 29 '11 at 18:10
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.