Preface
Sorting an array is a very common requirement, especially in the backend. Of course, the frontend also has this requirement.
Of course, there are ready-made methods for array sorting. It is the sort() method.
Let's start to 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 sorted from small to large');console.log(arr.sort(function(a,b){return ab}));console.log('sort method sorted from large to small');console.log(arr.sort(function(a,b){return ba}));console.log('sort method sorted from large to small');console.log(arr.sort(function(a,b){return ba}));The operation results are as follows:
It should be noted here that sort is sorted alphabetical by default. Therefore, when we arrange numbers, we need a custom function.
As above code
function(a,b){return ab}
This is a sorting function from small to large. It looks very simple, but I don't understand, so I will implement sorting according to my ideas~
My answer, 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 sorted from small to large');console.log('arrSortMinToMax(arr));console.log('for method sorted from large to small');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 the result of sorting from small to large in the array 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};}// Return the result of sorting from large to small in the array 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 operation results are shown in the figure below
Knowledge points in my method
1. When a function needs to return multiple pieces of data, it is more convenient to use the json object format. For example, the return {"minNum":minNum,"index":index} above;
2. If you use the method var arrOld = arr to copy an array, and operate on arrOld, it will affect the original array of arr. Because javascript is divided into primitive types and reference types (similar to java and c#). Array is a reference type. arrOld gets a reference, so the modification to arrOld will affect arr .
Methods for copying arrays (I) var arrOld = arr.concat(); , Principle: The concat() function is a function used to splice multiple arrays. This writing method is equivalent to splicing itself. That is, copying.
Methods for copying arrays (2) var arrOld = arr.slice(0). Principle: The slice() function is a function that intercepts arrays. Setting the value to 0 is to intercept them all, which is equivalent to copying.
3. The splice() method is used to insert, delete or replace elements of an array. Here is the feature that uses its deleted locations in the array.
4. The difference between my method and sort method.
My method does not modify the original array, while sort is a modification based on the original array.
My method returns a new array, and the original array has not disappeared or changed. (It seems to be the same as the above sentence...)
5. Sorting is a very, very basic and very important knowledge point in programming. Sort sorting is relatively low when executing a large amount of data. Of course, the efficiency of my method is also very low.
This article is reproduced at: http://blog.csdn.net/fungleo/article/details/51555590
The above is the entire content of the JavaScript sort array sort method and self-implementation sort method that the editor introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message. The editor will reply you in time. Thank you very much for your support for the Wulin Network website.