JavaScript array sorting Sort method and self-realization sorting Method Learning Summary by Fungleo

Source: Internet
Author: User
Tags javascript array

Objective

Sorting on an array is a very common requirement. Especially in the backend. Of course, the front end also has this demand.

Of course, array sorting is a ready-made method. That's the way it is sort() .

Let's start by looking at this.

Standard answer, sort method
vararr = [ $,98, the, $, -,6, -, the, -, -];console.log (' original array '); Console.log (arr); Console.log (' Sort method from small to large '); Console.log (Arr.sort ( function(A, B){returnA-B}); Console.log (' Sort method from big to small '); Console.log (Arr.sort ( function(A, B){returnB-A}));

The results of the operation are as follows:

It is important to note that the sort default is to sort alphabetically. So, when we're arranging numbers, we need a custom function.

As the above code

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

This is a sort function from small to large. It looks so simple, but I don't understand , so I'm going to sort it out according to my thoughts.

My answer to the For method sort
vararr = [ $,98, the, $, -,6, -, the, -, -];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){    varMinnum =Infinity, index =-1; for(vari =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 sorted results function arrsortmintomax(arr){    varArrnew = [];varArrold = Arr.concat (); for(vari =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){    varMaxnum =-Infinity, index =-1; for(vari =0; i < arr.length; i++) {if(Arr[i]>maxnum)            {maxnum = Arr[i];        index = i; }    };return{"Maxnum": Maxnum,"Index": index};}//return array from large to small sorted results function arrsortmaxtomin(arr){    varArrnew = [];varArrold = Arr.slice (0); for(vari =0; i < arr.length;        i++) {Arrnew.push (Arrmaxnum (arrold). Maxnum); Arrold.splice (Arrmaxnum (arrold). Index,1);    }; Console.log (arr)return(arrnew);}

The result of the operation is as shown

The point of knowledge in my method
    1. When a function needs to return more than one piece of data, it is convenient to use the JSON object format. As above return {"Minnum": Minnum, "index": index};
    2. If you use var arrold = arr to copy an array and, for Arrold , it will affect the original array of arr . Because The JavaScript is divided into primitive types and reference types (similar to Java, C #). Array is a reference type. Arrold gets a reference, so changes to Arrold will affect arr.
      1. methods for copying arrays (i) var arrold = Arr.concat (); , principle: concat () function is a function used to stitch multiple arrays, this write The law is equivalent to stitching themselves. The method of copying an array is copied.
      2. var arrold = arr.slice (0) , principle: slice () function is a function that intercepts an array, Setting a value of 0 is the total interception, which is equivalent to copying.
      3. The
    3. Splice () method is used to insert, delete, or replace elements of an array. Here is the attribute that is used to delete the specified position in the array.
    4. The difference between the
    5. my method and the sort method.
      1. My method does not modify the original array, and sort is a modification based on the original array.
      2. The
      3. my method returns a new array, and the original array does not disappear or change. (as if the above sentence is a meaning ...)
    6. Sorting is a very, very basic and very important point of knowledge in programming. Sort sorting is less efficient in the case 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 starting link. Otherwise, legal liability will be investigated.
Starting Address: http://blog.csdn.net/FungLeo/article/details/51555590

JavaScript array sorting Sort method and self-realization sorting Method Learning Summary by Fungleo

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.