JavaScriptSort's example of an incorrect usage _ javascript skills

Source: Internet
Author: User
This article mainly introduces an example of JavaScriptSort's error usage. This article analyzes an Sort instance to get this error usage and provides a solution, for more information, see the code of a colleague recently. the general process is to sort an array composed of objects. attribute a is used for sorting, attribute B is a preferred condition. when B is equal to 1, no matter what the value of a is, B is at the beginning. This is a very simple problem. The problem is that he uses sort twice to sort data in this sort. he first sorts data based on the attribute of a and then sorts data based on the value of B. The problem lies in the second sorting.

We assume that in the first sorting, the array has been sorted from large to small according to the attribute of, in the second time, we only need not to change the sequence of the original array (generally, 0 or-1 is returned in the method). we only need to mention the elements with B equal to 1 separately. However, this is actually related to the sorting algorithm selected by the language. the sort method built in javascript (together with other languages) uses a set of several sorting algorithms, sometimes the positions of the same elements cannot be consistent.

The following is an example from stackoverflow.

The code is as follows:


Var arrayToSort = [
{Name: 'A', strength: 1}, {name: 'B', strength: 1}, {name: 'C', strength: 1}, {name: 'D', strength: 1 },
{Name: 'e', strength: 1}, {name: 'F', strength: 1}, {name: 'G', strength: 1}, {name: 'H', strength: 1 },
{Name: 'I', strength: 1}, {name: 'J', strength: 1}, {name: 'K', strength: 1}, {name: 'L', strength: 1 },
{Name: 'M', strength: 1 },{ name: 'N', strength: 1 },{ name: 'O', strength: 1 },{ name: 'P', strength: 1 },
{Name: 'Q', strength: 1}, {name: 'R', strength: 1}, {name: 'S', strength: 1}, {name: 'T', strength: 1}
];

ArrayToSort. sort (function (a, B ){
Return B. strength-a. strength;
});

ArrayToSort. forEach (function (element ){
Console. log (element. name );
});

We will think that the value of the last element is still from a to t, but the actual running result is out of order, because the sort algorithm does not keep the order of the original array, that is, unstable.

So we should try our best to avoid this situation. in my colleague's example, it is feasible to merge the two sort logic at one time. if it must be divided into multiple sort, the order of the original array is recorded on the attribute of the element.

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.