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
- 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};
- 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.
- 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.
-
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. The
-
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. The difference between the
- my method and the
sort
method.
- My method does not modify the original array, and sort is a modification based on the original array.
The
- my method returns a new array, and the original array does not disappear or change. (as if the above sentence is a meaning ...)
- 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