up vote 2 down vote favorite

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!

link|flag

2 Answers

up vote 3 down vote accepted

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

link|flag
up vote 5 down vote

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

link|flag

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.