An error usage example for JavaScript Sort _javascript tips

Source: Internet
Author: User

A very strange problem with my colleague's code is that the approximate process is to sort an array of objects, where property A is used for sorting, and property B as a preferred condition, and when B equals 1, whatever a value is, it begins. This is a very simple question, the problem is that he uses the two sort implementation in this order, first according to A's attributes sorted, and then based on the value of B to sort. The problem is in the second order.

We take it for granted that in the first sort, the array has been sorted from large to small according to the properties of a, and in the second we simply do not move the order of the original array (usually written in the method to return 0 or-1), only consider the element B equals 1 alone to the front. But in fact this is related to the sort algorithm chosen by the language, and the built-in sort method of JavaScript (together with other languages) employs a collection of sorting algorithms that sometimes do not guarantee the same position of the same elements.

Here's an example from StackOverflow.

Copy Code code 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 assume that the value of the last element is still from a to T, but the actual result of the operation is random, because the sort algorithm does not preserve the order of the original array, or unstable.

Then we should try to avoid this, and in my colleague's case, it should be a good idea to combine the logic of the two sort in one time, if you have to divide it into multiple categories, then record the order of the original array on the attributes of the element.

Related Article

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.