Weird Behavior In Sorting Javascript Array
Solution 1:
ECMAScript neither dictates a specific algorithm, nor expects it to be stable (
Array.prototype.sort
). Stable sorting algorithms maintain the relative order of elements that appear to be "the same". To Array#sort two items appear the same when the comparison function returns 0. While InsertionSort and MergeSort (Apple and Mozilla) are stable, QuickSort (Google Chrome) is not (Issue 90). Chrome will sort arrays using InsertionSort if the array has 10 or less elements.So Safari and Firefox will sort
["sed", "dolor", "ipsum", "foo", "bar", "cat", "sit", "man", "lorem", "amet", "maecennas"]
(by character length) in a way that "sed" will retain first position, while Chrome will roll the dice and possibly prefer "cat" to take the pole position. The Chrome developers obviously like Kittens…
So implementing a stable algorithm, such as MergeSort yourself, if you find yourself in need.
check the full post here
Post a Comment for "Weird Behavior In Sorting Javascript Array"