6

It's hard to believe, but this looks like a bug in Google Chrome's Javascript engine. Am I missing something?

Chrome Javascript console session:

> x = [10, 1]
> x.sort()
[1, 10]
> // OK.  But now try this.
> x = [10, 2]
> x.sort()
[10, 2]

It didn't sort it!

I'm currently running Version 24.0.1312.57 m

4
  • 1
    Strange. I've tested it in FF and Opera - same result. Commented Feb 26, 2013 at 8:03
  • Many thanks to the people who took the time to answer. In hindsight, it's "obvious" what's going on. :) Commented Feb 26, 2013 at 9:08
  • possible duplicate of sort not working with integers? Commented Jul 31, 2013 at 5:17
  • this is the elephant in the room. Commented Jan 9, 2014 at 14:11

5 Answers 5

11

array.sort() sorts the array in lexicographical order. That means, the values of the array are interpreted as Strings and sorted like Strings (alphabetically), not like integers.

This behavior is also described here: http://www.javascriptkit.com/javatutors/arraysort.shtml

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. That explains it.
7

For those who came here figuring out what the heck is wrong with sorting in Chrome, here's an example of what unstable sort is: https://jsfiddle.net/wujpw8bo/

How to fix it:

Unstable sorting algorithms can be specially implemented to be stable. One way of doing this is to artificially extend the key comparison, so that comparisons between two objects with otherwise equal keys are decided using the order of the entries in the original input list as a tie-breaker. Remembering this order, however, may require additional time and space. https://en.wikipedia.org/wiki/Sorting_algorithm#Stability

1 Comment

Doesn't have anything to do with the question from the opener - but I learned something today. Thanks man! Upvote.
5

I think MDN has Explained it well Source MDN Array.sort()

The sort() method sorts the elements of an array in place and returns the array. The sort is not necessarily stable. The default sort order is according to string Unicode code points.

arr.sort([compareFunction])

compareFunction Optional

  1. Specifies a function that defines the sort order. If omitted, the array is sorted according to each character's Unicode code point value, according to the string conversion of each element.
  2. If compareFunction is not supplied, elements are sorted by converting them to strings and comparing strings in Unicode code point order. For example, "Cherry" comes before "banana". In a numeric sort, 9 comes before 80, but because numbers are converted to strings, "80" comes before "9" in Unicode order.
var scores = [1, 10, 2, 21]; 
scores.sort(); // [1, 10, 2, 21]
// Watch out that 10 comes before 2,
// because '10' comes before '2' in Unicode code point order.

To compare numbers instead of strings, the compare function can simply subtract b from a. The following function will sort the array ascending:

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

Example:

function compareNumbers(a, b) {
  return a - b;
}
var scores = ['1', '010', '200', '110']; 
scores.sort(compareNumbers);

O/P: Array [ "1", "010", "110", "200" ]

1 Comment

good explanation! In a numeric sort, 9 comes before 80, but because numbers are converted to strings, "80" comes before "9" in Unicode order.
1

You should use the following method:

abc =[10,2,4,1]; abc.sort(function( a , b ){
  return a-b;
});

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

Comments

-1

You're missing brackets on your second sort()

x.sort()

Edit: Just tried it myself and even with brackets its not working.

Check these questions out. Maybe they'll fix your problem.

Sorting an array of objects in Chrome

Sorting Javascript Array with Chrome?

Edit2: This works as expected:

var x;
x = [10, 2]; 
alert(x); // returns 10, 2
x.sort ( function( a , b ){
  return a-b;
});
alert(x); // returns 2,10

1 Comment

This is just a typo, and not related to the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.