JavaScript sort array sorting method and self-implementation sorting method summary _javascript Skills

Source: Internet
Author: User

Objective

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 based on my ideas, to achieve the sort bar ~

My answer to the For method sort

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 results function Arrsortmintomax (arr) {var arrnew = []; var arrold = Arr.concat (); for (var i = 0; i < Arr.le Ngth;
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 (a)
Rr[i]>maxnum) {maxnum = Arr[i]; index = i;};
return {"Maxnum": Maxnum, "index": index}; //return array from large to small sort result function arrsortmaxtomin (arr) {var arrnew = []; var arrold = arr.slice (0); for (var i = 0; i < Arr.le Ngth; 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 points of knowledge in my approach

1. 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};

2. 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 3.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.

4. 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 ...)

5. Sorting is a very, very basic and very important point of knowledge in programming. Sort sort is inefficient in the execution of large amounts of data. Of course, my method is also very inefficient.

This article is reproduced in: http://blog.csdn.net/fungleo/article/details/51555590

The above is the entire contents of a summary of the JavaScript sort array sorting method and the self implementation sorting method described in the small series, hope to be helpful to everyone, if you have any questions welcome to my message, small series will promptly reply to everyone, here also thank you for the cloud Habitat Community website support.

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.