0

i have gone through this article about Array.prototype.sort().Sort() function can behave differently according to the availability of compareFunction.For strings it does sorting using UNICODE value.But Here in this particular example an array contains two different elements having same first three letters.My question is how compareFunction decides which to go first in a situation like this??

var numbers = ['Hammer',"Hamburger"];
numbers.sort(function(a, b) {
  return a - b;
});
console.log(numbers); //['Hammer','Hamburger']
AL-zami
  • 8,902
  • 15
  • 71
  • 130
  • It does not at all, because `'Hammer' - 'Hamburger'` is `NaN`. Your compare function is invalid for this array, which are not `numbers`. You'll need a valid string comparison function. – Bergi Jan 19 '15 at 13:04

4 Answers4

2

I think you are getting problem because in compareFunction you are saying

a-b; 

remember "str1"-"str2" will return a NaN. So you are not going to get expected results.

Say like bellow if you want to get it sorted in ascending order

a>b;

OR

a.localeCompare(b);

Full Code

var numbers = ['Hammer',"Hamburger"];
numbers.sort(function(a, b) {
  return a.localeCompare(b);
});
console.log(numbers); //["Hamburger", "Hammer"]
Mritunjay
  • 25,338
  • 7
  • 55
  • 68
  • It might affect his array in an unspecified way. It might completely shuffle it, or the sort might even not terminate at all. – Bergi Jan 19 '15 at 13:05
  • @Bergi can you explain a bit deep, I didn't get why this solution is bad? – Mritunjay Jan 19 '15 at 13:14
  • Just wanted to say that "*is not going to effect your array*" is inaccurate :-) Your solution is fine, though indeed the `a>b` you previously had [wasn't enough](http://stackoverflow.com/q/24080785/1048572). – Bergi Jan 19 '15 at 15:44
1

This method does not apply for string values

To compare numbers instead of strings, the compare function can simply subtract b from a:

function compareNumbers(a, b) {
    return a - b;
}

That being said, this should be used for numbers only and does not apply for strings.

Sorting non-ASCII characters

"For sorting strings with non-ASCII characters, i.e. strings with accented characters (e, é, è, a, ä, etc.), strings from languages other than English: use String.localeCompare. This function can compare those characters so they appear in the right order.:"

var items = ['réservé', 'premier', 'cliché', 'communiqué', 'café', 'adieu'];
items.sort(function (a, b) {
     return a.localeCompare(b);
});
// items is ['adieu', 'café', 'cliché', 'communiqué', 'premier', 'réservé']

Otherwise, simply use the normal .sort() and don't worry about the implementation unless it appears odd to you

var fruit = ['apples', 'bananas', 'Cherries'];
fruit.sort(); // ['Cherries', 'apples', 'bananas'];
Jonast92
  • 4,964
  • 1
  • 18
  • 32
1

You can do like this with String array:

function compareString(a, b)
{
       var lowera = a.toLowerCase();
         var lowerb = b.toLowerCase();
         if (lowera < lowerb){
            return -1;
         }else if (lowera > lowerb){
           return  1;
         }else{
           return 0;
         }
}

var numbers = ['Hammer',"Hamburger"];
numbers.sort(compareString);
console.log(numbers); //['Hamburger','Hammer']
Inanda Menezes
  • 1,796
  • 13
  • 17
0

.sort(function(){}) behaves like this (returned variables will be called result):

1) result < 0 - second element is bigger than first;

2) result = 0 - second element is equal to first element;

3) result > 0 - second element is smaller than first element;

So this algorithm only works for numbers, when comparing string, "str1" - "str2" will return NaN

Justinas
  • 41,402
  • 5
  • 66
  • 96