sort () method is one of the most powerful array of methods.
Sorting an array
sort () method to alphabetically sort the array:
Examples
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.sort (); // for sorting elements in fruits
Reverse Array
reverse () method of inverting elements in an array.
You can use it to sort the array in descending order:
Examples
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.sort (); // for sorting elements in fruits fruits.reverse (); // reverse the order of elements
By default, Sort () function string values are sorted in order.
This function is suitable for strings ( "Apple" will be sorted before "Banana").
However, if the numbers to sort character strings, then "25" is greater than "100", since "2" is greater than "1."
For this reason, the Sort () method can produce incorrect results when sorted values.
Let's fix this problem by a ratio function:
Examples
var points = [40, 100, 1, 5, 25, 10]; points.sort(function(a, b){return a - b});
Use the same technique to the array in descending order:
Examples
var points = [40, 100, 1, 5, 25, 10]; points.sort(function(a, b){return b - a});
The ratio function
Objective To compare another function is to define the sort order.
The comparison function should return a negative, zero or positive, depending on the parameters:
function(a, b){return a-b}
When time () Sort function compares two values, the values are sent to the comparison function, and based on the value returned (negative, zero or positive) sorts the values.
Example:
When comparing the 40 and 100, Sort () method calls a function of the comparison function (40,100).
This function computes 40-100, and returns -60 (negative).
Sort function will sort 40 to a value lower than 100.
You can use the following code fragments and the test values in alphabetical order:
<Button onclick = "myFunction1 ()"> sorted alphabetically </ button> <Button onclick = "myFunction2 ()"> sorted in numerical order </ button> <p id="demo"></p> <script> var points = [40, 100, 1, 5, 25, 10]; document.getElementById("demo").innerHTML = points; function myFunction1() { points.sort(); document.getElementById("demo").innerHTML = points; } function myFunction2() { points.sort(function(a, b){return a - b}); document.getElementById("demo").innerHTML = points; } </script>
Random order sorted array
Examples
var points = [40, 100, 1, 5, 25, 10]; points.sort(function(a, b){return 0.5 - Math.random()});
Find the highest (or lowest) array values
JavaScript does not provide an array to find the maximum or minimum values in the array of built-in functions.
However, after the array is sorted, you can use the index to the highest or lowest value.
Ascending order:
Examples
var points = [40, 100, 1, 5, 25, 10]; points.sort(function(a, b){return a - b}); // now points [0] contains the lowest value // and points [points.length-1] contains the highest value
In descending order:
Examples
var points = [40, 100, 1, 5, 25, 10]; points.sort(function(a, b){return b - a}); // now points [0] contains the highest value // and points [points.length-1] contains the lowest value
If you only need to find the maximum or minimum value, the entire array sort is very inefficient method.
Use Math.max array ()
You can use Math.max.apply to find the highest value in the array:
Examples
function myArrayMax(arr) { return Math.max.apply(null, arr); }
Math.max.apply ([1, 2, 3 ]) is equal to Math.max (1, 2, 3) .
Use Math.min array ()
You can use Math.min.apply to find the lowest values in the array:
Examples
function myArrayMin(arr) { return Math.min.apply(null, arr); }
Math.min.apply ([1, 2, 3 ]) is equal to Math.min (1, 2, 3) .
My Min / Max JavaScript method
The fastest solution is to use "home-made" approach.
This function iterates through the array, compare each value with the highest values found:
Examples (Find Max)
function myArrayMax(arr) { var len = arr.length var max = -Infinity; while (len--) { if (arr[len] > max) { max = arr [len]; } } return max; }
This function iterates through the array, compare each value with the lowest values found:
Examples (Find Min)
function myArrayMin(arr) { var len = arr.length var min = Infinity; while (len--) { if (arr[len] < min) { min = arr [s]; } } Return min; }
Sorting an array of objects
JavaScript array of objects often include:
Examples
was Cars = [ {type:"Volvo", year:2016}, {type:"Saab", year:2001}, {type:"BMW", year:2010}];
Even if the object data types have different properties, sort () method can still be used to sort the array.
The solution is to compare the attribute value comparison function by:
Examples
cars.sort(function(a, b){return a.year - b.year});
Compare string property slightly more complex:
Examples
cars.sort(function(a, b){ var x = a.type.toLowerCase(); var y = b.type.toLowerCase(); if (x < y) {return -1;} if (x > y) {return 1;} return 0; });
Source: www.sysoft.net.cn, plus v: 15844800162 depth exchange