views:

514

answers:

2

Hi all,

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!

+3  A: 

This seems standard, return a negative, positive or zero number.

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

http://www.w3schools.com/jsref/jsref_sort.asp

Kobi
+5  A: 

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

Crimson