Home > Web Front-end > JS Tutorial > body text

Sharing the method of sorting array Array.sort() in JavaScript

黄舟
Release: 2017-03-24 15:13:25
Original
1260 people have browsed it

This article mainly introduces the sorting method of array.sort() in JavaScript. It has a very good reference value. Let’s take a look at it with the editor.

The sort() method of arrays in JavaScript is mainly used to sort the elements of the array. Among them, the sort() method has an optional parameter. However, this parameter must be a function. When calling the sort() method of an array, if no parameters are passed, the elements in the array will be sorted in alphabetical order (character encoding order). If you want to sort according to other criteria, you need to pass a parameter and it is a function. This function Compares two values ​​and returns a number describing the relative order of the two values.

1. Sort the numerical array from small to large.

Code:

var arr = [22,12,3,43,56,47,4];
arr.sort();
console.log(arr); // [12, 22, 3, 4, 43, 47, 56]
arr.sort(function (m, n) {
 if (m < n) return -1
 else if (m > n) return 1
 else return 0
});
console.log(arr); // [3, 4, 12, 22, 43, 47, 56]
Copy after login

2. Perform case-insensitive alphabetical sorting on the string array.

Code:

var arr = [&#39;abc&#39;, &#39;Def&#39;, &#39;BoC&#39;, &#39;FED&#39;];
console.log(arr.sort()); // ["BoC", "Def", "FED", "abc"]
console.log(arr.sort(function(s, t){
 var a = s.toLowerCase();
 var b = t.toLowerCase();
 if (a < b) return -1;
 if (a > b) return 1;
 return 0;
})); // ["abc", "BoC", "Def", "FED"]
Copy after login

3. Sort the array containing the object, and require it to be sorted according to the age of the object. Arranged in descending order

Code:

var arr = [{&#39;name&#39;: &#39;张三&#39;, age: 26},{&#39;name&#39;: &#39;李四&#39;, age: 12},{&#39;name&#39;: &#39;王五&#39;, age: 37},{&#39;name&#39;: &#39;赵六&#39;, age: 4}];
var objectArraySort = function (keyName) {
 return function (objectN, objectM) {
  var valueN = objectN[keyName]
  var valueM = objectM[keyName]
  if (valueN < valueM) return 1
  else if (valueN > valueM) return -1
  else return 0
 }
}
arr.sort(objectArraySort(&#39;age&#39;))
console.log(arr) // [{&#39;name&#39;: &#39;王五&#39;, age: 37},{&#39;name&#39;: &#39;张三&#39;, age: 26},{&#39;name&#39;: &#39;李四&#39;, age: 12},{&#39;name&#39;: &#39;赵六&#39;, age: 4}]
Copy after login

The above is the detailed content of Sharing the method of sorting array Array.sort() in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template