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

JS array sorting

不言
Release: 2018-04-03 18:01:48
Original
2513 people have browsed it


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. Is a function that compares two values ​​and returns a number that describes the relative order of the two values.

1. Sort the numerical array from small to large.

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  //m < n 返回-1是从小到大排序,返回1是从大到小排序
 else if (m > n) return 1
 else return 0});
console.log(arr); // [3, 4, 12, 22, 43, 47, 56],按照数值大小排序
Copy after login
Copy after login

2. Perform a case-insensitive alphabetical sort on a string array

var arr = ['abc', 'Def', 'BoC', 'FED'];
console.log(arr.sort()); // ["BoC", "Def", "FED", "abc"],按首个字母在ASIIC中出现的位置排序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
Copy after login

3. Sort an array containing objects, requiring descending order based on age in the object Sequential arrangement

var arr = [{'name': '张三', age: 26},{'name': '李四', age: 12},{'name': '王五', age: 37},{'name': '赵六', 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('age'))
console.log(arr) // [{'name': '王五', age: 37},{'name': '张三', age: 26},{'name': '李四', age: 12},{'name': '赵六', age: 4}]
Copy after login
Copy after login

             

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. Is a function that compares two values ​​and returns a number that describes the relative order of the two values.

1. Sort the numerical array from small to large.

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  //m < n 返回-1是从小到大排序,返回1是从大到小排序
 else if (m > n) return 1
 else return 0});
console.log(arr); // [3, 4, 12, 22, 43, 47, 56],按照数值大小排序
Copy after login
Copy after login

2. Perform a case-insensitive alphabetical sort on a string array

var arr = ['abc', 'Def', 'BoC', 'FED'];
console.log(arr.sort()); // ["BoC", "Def", "FED", "abc"],按首个字母在ASIIC中出现的位置排序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
Copy after login

3. Sort an array containing objects, requiring descending order based on age in the object Arrange in order

var arr = [{'name': '张三', age: 26},{'name': '李四', age: 12},{'name': '王五', age: 37},{'name': '赵六', 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('age'))
console.log(arr) // [{'name': '王五', age: 37},{'name': '张三', age: 26},{'name': '李四', age: 12},{'name': '赵六', age: 4}]
Copy after login
Copy after login

Related recommendations:

How to operate js arrays to remove duplicates

js array knowledge summary sharing

The above is the detailed content of JS array sorting. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!