Share Array. sort () sorting methods in JavaScript

Source: Internet
Author: User
This article describes how to sort arrays. sort () in JavaScript. It has good reference value. Let's take a look at it below. This article mainly introduces the sorting method of Array. sort () in JavaScript. It has good reference value. Let's take a look at it together with the small Editor.

The sort () method of arrays in JavaScript is mainly used to sort the elements of arrays. The sort () method has an optional parameter. However, this parameter must be a function. When an array calls the sort () method, if it is not passed, it sorts the elements in the array in alphabetical order (character encoding order). If you want to sort the elements according to other criteria, you need to pass a parameter and it is a function. This function must compare two values and return a number indicating the relative sequence of the two values.

1. Sort the number array in ascending order.

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]

2. Perform case-insensitive alphabetic sorting on string arrays.

Code:

var arr = ['abc', 'Def', 'BoC', 'FED'];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"]

3. Sort the array containing objects in the ascending order of age.

Code:

Var arr = [{'name': 'zhang san', age: 26}, {'name': 'Li si', age: 12}, {'name ': 'wang 5', age: 37}, {'name': 'zhao liu', 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': 'wang 5', age: 37}, {'name': 'zhang san', age: 26 },{ 'name ': 'lily', age: 12}, {'name': 'zhao liu', age: 4}]

The above is the details shared by the Array. sort () sorting method in JavaScript. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.