Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the sort () method in javascript

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article is a detailed introduction to "how to use the sort() method in javascript". The content is detailed, the steps are clear, and the details are properly handled. I hope this article "how to use the sort() method in javascript" can help you solve your doubts. Let's go deeper and learn new knowledge together with the ideas of Xiaobian.

In javascript, the sort() method is used for array sorting, which can sort array elements according to certain conditions, and the syntax format is "arrayObject.sort(sortby)". Sorts the elements of the array alphabetically if no arguments are passed when the sort() method is called.

Operating environment of this tutorial: Windows 7 system, Javascript version 1.8.5, Dell G3 computer.

Sort arrays using sort()

The sort() method can sort array elements according to certain conditions. Sorts the elements of the array alphabetically if no arguments are passed when the sort() method is called.

var a = ["a","e","d","b","c"]; //Defines array a.sort(); //sorts elements alphabetically console.log (a); //returns array [a,b,c,d,e]

When using the sort() method, you should pay attention to the following issues.

1)Alphabetical order is actually arranged according to the order of letters in the character coding table, and each character has a unique number in the character table.

2)If the element is not a string, the sort() method attempts to convert all array elements to strings for comparison purposes.

3)The sort() method performs a bitwise comparison based on element values rather than sorting based on the number of strings.

var a = ["aba","baa","aab"]; defines array a.sort(); //sorts elements alphabetically console.log (a); //returns array [aab,aba,baa]

In sorting, the first character of each element is compared, and if the first character is the same, the second character is compared, and so on.

4)In any case, undefined elements of an array are sorted at the end.

5)The sort() method sorts on top of the original array and does not create a new array.

The sort() method not only sorts alphabetically, but can also perform operations in other orders. In this case, you must provide the method with a function argument that compares two values and returns a number describing their relative order. The sorting function should have two parameters a and b, and its return value is as follows.

If a is less than b according to the custom criteria, a should appear before b in the sorted array, then a value less than 0 is returned.

If a equals b, it returns 0.

If a is greater than b, a value greater than 0 is returned.

Examples 1

In the following example, the size of each element in the array is compared according to the sort function, and sorting is performed in descending order.

function f(a,b) { //sort function return (a - b); //return comparison parameters}var a = [3,1,2,4,5,7,6,8,0,9]; //define array a.sort(f); //sort numbers from small to large console.log (a); //return array [0,1,2,3,4,5,6,4,7,8,9]

If executed in descending order, the return value is negated. The code is as follows:

function f(a,b) { //sort function return -(a - b); //negates and returns comparison parameters}var a = [3,1,2,4,5,7,6,8,0,9]; //defines array a.sort(f); //sorts numbers from small to large console.log (a); //returns array [9,8,7,6,5,4,3,2,1,0]

Example 2

Arranges arrays according to parity properties.

Sort() is used flexibly, mainly for function sorting comparison. For example, if you sort an array according to odd and even numbers, you only need to determine whether two parameters in the order function are odd and even numbers and determine the order of arrangement.

function f(a, b) { //sort function var a = a % 2; //Get parity of parameter a var b = b % 2; //get parity of parameter b if (a == 0) return 1; //if argument a is even, rank left if (b == 0) return -1; //if argument b is even, rank to the right}var a = [3,1,2,4,5,7,6,8,0,9]; //define array a.sort(f); //sort numbers from largest to smallest console.log (a); //return array [3,1,5,7,9,0,8,6,4,2]

Sort() method in the call sorting function, for each element value passed to the sorting function, if the element value is even, then keep its position unchanged; if the element value is odd, then reverse the display order of parameters a and b, so as to achieve the array of all elements in the parity sort. If you want even numbers to come first and odd numbers to come last, you just need to take the return value. The sorting function is as follows.

function f(a, b) { var a = a % 2; var b = b % 2; if (a == 0) return -1; if (b == 0) return 1;}

Example 3

Case-insensitive sort string.

Normally, sorting strings is case-sensitive because each upper and lower case letter has a different order in the character encoding table, with upper case letters larger than lower case letters.

var a = ["aB", "Ab", "Ba", "bA"]; //Defines array a.sort(); //Default method sort console.log (a); //Returns array ["Ab", "Ba", "aB", "bA"]

Capital letters are always on the left. If you want lowercase letters to always be on the left, you can design:

function f(a ,b) { return (a

< b);}var a = ["aB", "Ab", "Ba", "bA"]; //定义数组a.sort(); //默认方法排序console.log(a); //返回数组["Ab", "Ba", "aB", "bA"] 对于字母比较大小时,JavaScript 是根据字符编码大小来决定的,当为 true 时,则返回 1;为 false 时,则返回 -1。 如果不希望区分大小写,大写字母和小写字母按相同顺序排列,可以设计: function f(a, b) { var a = a.toLowerCase; var b = b.toLowerCase; if (a < b) { return 1; } else { return -1; }}var a = ["aB", "Ab", "Ba", "bA"]; //定义数组a.sort(); //默认方法排序console.log(a); //返回数组["aB", "Ab", "Ba", "bA"] 如果要调整排列顺序,则设置返回值取反即可。 示例4 把浮点数和整数分开显示。 function f(a, b) { //排序函数 if (a >

Math.floor(a)) return 1; //transpose if a is a floating point if (b > Math.floor(b)) return -1; //if b is a floating point, transpose}var a = [3.5555, 1.23456, 3, 2.1111,5, 7, 3]; //define array a.sort(f); //filter console.log(a); //return array [3,5,7,3,2.111,1.23456,3.5555]

If you want to adjust the sort order, you can set the return value to be reversed.

Read here, this article "how to use the sort() method in javascript" article has been introduced, want to master the knowledge points of this article also need to be used by ourselves to understand, if you want to know more about the relevant content of the article, welcome to pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report