7

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.

3
  • @Tim Cooper's answer is the only one you need. Commented Mar 29, 2011 at 17:54
  • of course you can also do this handy little shortcut .......................................................... (sortArray.sort())[(dir === 'asc' ? 'reverse' : 'slice' )]() Commented Mar 29, 2011 at 18:27
  • @mVChr: DEMO Commented Mar 29, 2011 at 18:34

4 Answers 4

12

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

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

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

EDIT: Updated to fix comparison issue according to comment.

See example →

Sign up to request clarification or add additional context in comments.

4 Comments

I accepted yours for the toLowerCase() add on...though everyone deserves a +1.
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 is right. This should be: if (a.toLowerCase() === b.toLowerCase()) { return 0 } else { return a.toLowerCase() < b.toLowerCase() ? -1 : 1 }
@Mike && digant-c-kasundra I updated to fix the issue. Thanks!
7

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

1 Comment

Thanks everyone, obviously I misread the docs on that one - : D
7

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});
    }

Comments

6

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"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.