Sorting Javascript Array with Chrome?

25,745

Solution 1

The comparer function should return a negative number, positive number, or zero (which is a convention across programming languages).

myArray.sort(function(a, b) {
  return a-b;
});

Full description is here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#description

Solution 2

The behavior of Chrome is correct :)

The ECMA standards require the function being passed to sort() to return a number greater than 0, less than 0 or equal to 0. However, the function you have defined returns true / false. ECMA standards state that for a function which does not behave as expected, the implementation depends on the client.

Read this

Solution 3

Because of what the ECMA Standard covers about sort arrays (in a very simplified way):

  • If in the comparison receives 1 A descend one position.
  • If receives -1 maintain the position and define the superior ranking toward the B.
  • If receives 0 does nothing.

The safest way to guarantee the same behavior in all browser is :

// descending order
abc =[10,2,4,1]; 
abc.sort(function( a , b ){
  return a > b ? -1 : 1;
});

// ascending order
abc.sort(function( a , b ){
  return a > b ? 1 : -1;
});

For primitive objects is posible to use the short version

// descending order
abc.sort(function( a , b ){
  return b - a; 
});

// ascending order
abc.sort(function( a , b ){
  return a - b; 
});

if you have next array:

var items = [
       { name: 'Edward', value: 21 },
       { name: 'Sharpe', value: 27 },
       { name: 'And', value: 31 },
       { name: 'The', value: -12 },
       { name: 'Zeros', value: 37 },
       { name: 'Magnetic', value: 37 } 
]

The right way is:

 items.sort(function( a , b ){
   var result = a == b ? 0 : b > a ? -1 : 1
   if(result === 0)
   {
     // implement a tight break evaluation
   }
   return result ;
  });

This is the right way because the way that the browser iterates is not defined in the ECMA standard and browser may iterate in different ways. For instance most browsers iterate from top to bottom, but chrome iterates the 1st element with the last and go the way up. So in case of a tight may result different result of most browsers.

Solution 4

I think, the correct reason is here: Sorting an array of objects in Chrome, more specifically, this post.

Post reading that, if you feel the need to implement your own array sorting function, you may have a look at: http://en.literateprograms.org/Merge_sort_%28JavaScript%29

Share:
25,745
jantimon
Author by

jantimon

https://github.com/jantimon/html-webpack-plugin Follow me: @jantimon

Updated on May 09, 2021

Comments

  • jantimon
    jantimon about 3 years

    Is there a way to sort an array using Chrome?


    Using the sort function does not work as seen in this example:

    var myArray = [1,4,5,3,2];
    
    myArray.sort ( function( a , b ){
      return b>a
    });
    
    for ( var i = 0; i < myArray.length; i++ )
    {
      document.write( myArray[i] )
    }
    

    Firefox / IE / Opera / Safri output: 54321

    Chrome output: 53241

    jsBin example


    Thanks for your time!