Reputation: 2237
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
Upvotes: 6
Views: 9455
Reputation: 3826
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
Upvotes: 7
Reputation: 1580
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
- 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.
- 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" ]
Upvotes: 5
Reputation: 6334
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
Upvotes: 11
Reputation: 28722
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
Upvotes: 1
Reputation: 4370
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
Upvotes: -1