AL-zami
AL-zami

Reputation: 9066

what is the mechanism going on inside this sort function

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']

Upvotes: 0

Views: 90

Answers (4)

Mritunjay
Mritunjay

Reputation: 25882

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"]

Upvotes: 2

Inanda Menezes
Inanda Menezes

Reputation: 1804

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']

Upvotes: 1

Jonast92
Jonast92

Reputation: 4967

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'];

Upvotes: 1

Justinas
Justinas

Reputation: 43451

.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

Upvotes: 0

Related Questions