简体   繁体   中英

Is this a bug in Array.sort()?

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

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

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

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" ]

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM