A study of JavaScript array sort sort method and self-realization sort method by Fungleo

Source: Internet
Author: User
Tags array sort javascript array
Preface

A very common requirement for sorting an array. Especially at the back end. Of course, the front end also has this demand.

Of course, array ordering is a ready-made method. The sort () method.

Let's take a look at this first. Standard answer, sort method

var arr = [45,98,67,57,85,6,58,83,48,18];
Console.log (' original array ');
Console.log (arr);
Console.log (' Sort method from small to large sort ');
Console.log (Arr.sort (function (a,b) {return a-b}));
Console.log (' Sort method from large to small sort ');
Console.log (Arr.sort (function (a,b) {return b-a}));

The results of the operation are as follows:

Note that sort defaults are sorted alphabetically. So, we need a custom function when we're arranging numbers.

As the code above

function (a,b) {return a-b}

This is a small to large sort function. Looks so simple, but I do not understand , so, I am based on my ideas to achieve the sort bar ~ My answer, for method sorting

var arr = [45,98,67,57,85,6,58,83,48,18];
Console.log (' original array ');
Console.log (arr);
Console.log (' for method from small to large sort ');
Console.log (Arrsortmintomax (arr));
Console.log (' for method from large to small sort ');
Console.log (Arrsortmaxtomin (arr));
    Find the smallest value in the array function Arrminnum (arr) {var minnum = Infinity, index =-1;
            for (var i = 0; i < arr.length i++) {if (arr[i]<minnum) {minnum = Arr[i];
        index = i;
    }
    };
return {"Minnum": Minnum, "index": index};
    //return array from small to large sort result function Arrsortmintomax (arr) {var arrnew = [];
    var arrold = Arr.concat ();
        for (var i = 0; i < arr.length i++) {Arrnew.push (Arrminnum (arrold). Minnum);
    Arrold.splice (Arrminnum (arrold). index,1)};
return (arrnew);
    //Find the largest value in the array function Arrmaxnum (arr) {var maxnum =-infinity, index =-1;
            for (var i = 0; i < arr.length i++) {if (arr[i]>maxnum) {maxnum = Arr[i];
        index = i;
    }
    }; return {"Maxnum": Maxnum, "INdex ": Index};
    //returns array from large to small sort result function arrsortmaxtomin (arr) {var arrnew = [];
    var arrold = arr.slice (0);
        for (var i = 0; i < arr.length i++) {Arrnew.push (Arrmaxnum (arrold). Maxnum);
    Arrold.splice (Arrmaxnum (arrold). index,1);
    };
Console.log (arr) return (arrnew); }

The results of the operation are shown in the following figure
The knowledge point in my method when a function needs to return more than one piece of data, it is convenient to use the JSON object format. Such as above returns {"Minnum": Minnum, "index": index}; If you use var Arrold = arr This method to copy an array, and if you operate on arrold, it will affect the original array of arr. Because JavaScript is divided into primitive types and reference types (similar to Java, C #). Array is a reference type. Arrold gets a reference, so the modification of arrold affects arr.
Methods of copying arrays (i) var arrold = Arr.concat (); , principle: the concat () function is used to splice multiple arrays of functions, which is equivalent to stitching themselves. That's the copy. Methods of copying Arrays (ii) var arrold = arr.slice (0), principle: the slice () function is a function of intercepting an array, setting a value of 0, is all intercept, equivalent to copy. The splice () method inserts, deletes, or replaces the elements of an array. This is the attribute that is used to delete the specified position in the array. The difference between my method and the sort method.
My method does not modify the original array, and sort is the modification on the basis of the original array. My method returns a new array, and the original array does not disappear or change. (as if with the above sentence is a meaning ...) sorting is a very very basic and very important point of knowledge in programming. Sort sorting is inefficient in the execution of large amounts of data. Of course, my method is also very inefficient.

This article is original by Fungleo, allow reprint. But reprint must be signed by the author, and keep the article first link. Otherwise, the law will be held accountable.
Starting Address: http://blog.csdn.net/FungLeo/article/details/51555590

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.