Chrome Google browser js code array.sort sorting Bug Disorder Solution

Source: Internet
Author: User

Phenomenon

The code is as follows:

var list = [{n: "a", v:1}, {n: "B", v:1}, {n: "C", v:1}, {n: "D", v:1}, {n: "E", v:1}, {n: "F", V : 1}, {n: "G", v:1}, {n: "H", v:1}, {n: "I", v:1}, {n: "J", v:1}, {n: "K", V:1 },];        List.sort (function  (A, b) {            return a.v- b.v;        });          for (var i = 0; i < list.length; i++) {            console.log (LIST[I].N);        }

It's easy to just set an array of objects and sort them by the V field in the Sort method, which can be easily overlooked, because the normal thinking is that as long as all V is equal, the result is the same as before, but ...

The results are as follows:

====ie11====

Firefox = = = = =

====chrome====

As you can see, IE and Firefox are no problem, but Chrome has become a mess.

After consulting the information, there seems to be little practical solution to the problem on the Internet. It is said that Google developers think this is not a bug is not resolved, because the reason for the V8 engine, in order to efficiently sort, called the unstable sort. There are cattle on the internet said that the array more than 10 will call another sort method (insert sort), 10 is the use of the fast sorting algorithm, in order to submit efficiency, so this situation occurs.

Online someone has given a way to force the difference in the same situation (when the order is the same, make a smaller than B):

List.sort (function  (A, b) {            return A.V-B.V | |-1;        });

The method is tested to be invalid. The cause may be that the return values are the same.

Very distressed, so continue to think about it, since the return value of the same will occur this problem, how can the return value is not the same as the result of the order is correct? So after some thinking, think of a value: index, yes! is to sort by index, and if the order is the same then the order of the indexes is the order in which they were sorted, so the code changes to:

List.sort (function  (A, b) {            return a.v-b.v | | list.indexof (a)-list.indexof (b);        } );

The test results are no problem, the same as we expected!

"Solution"

So the correct result is: Add a sentence | | List.indexof (a)-list.indexof (b) Perfect solution

For example:

List.sort (function  (A, b) {            return a.v-b.v | | list.indexof (a)-list.indexof (b);        } );

Chrome Google browser js code array.sort sorting Bug Disorder Solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.