In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to sort arrays in es6". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to sort arrays in es6.
The sorting method of the es6 array is "sort ()". The sort () method is used to sort the elements of an array, either alphabetically or numerically, in ascending or descending order, by default, in ascending alphabetical order; the method has an optional parameter, which must be a function, and the syntax "array.sort (callback (aMagneb)").
The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.
The sort () method is used to sort the elements of the array.
The sort order can be alphabetic or numeric, in ascending or descending order.
The default sort order is ascending alphabetically.
Where the sort () method has an optional parameter. However, this parameter must be a function. When the array calls the sort () method, if no arguments are passed, the elements in the array are sorted alphabetically (character encoding order). If you want to sort by other criteria, you need to pass an argument and be a function, which compares two values and returns a number that describes the relative order of the two values.
Syntax:
The parameter array.sort (callback (aformab)) describes callback (aformab)
Optional. Specify the sort order. Must be a function.
Return value: Array type, which is a reference to the array. Note that the array is sorted on the original array and does not make a copy.
Example:
/ / basic use of sort let arr = [8, 1, 4, 3, 7, 9] let Arr = [21, 55, 29, 105, 45] console.log (arr.sort ()) / / [1, 3, 4, 7, 8, 9] console.log (Arr.sort ()) / / [105,21,29,45,55]
As can be seen from the above code: the sort () method can only correctly sort the array within 0-9, and the array items with more than two digits give a return value, but they are not the result of sorting. This is because sort () is done internally and sorts by ASCL code, not by numeric size. Then this method can not even carry out formal sorting of numbers with more than two digits. What is the difference between this method and salted fish?
Here's the point: sort () can accept a callback with two formal parameters, that is, an and b are two elements that are about to be compared in size, and there must be a return value.
When the return value of callback is positive, then b will be arranged before a
When the return value of callback is negative, then a will be arranged before b
When the return value of callback is 0, then the positions of an and b remain unchanged
Each time sort executes, it swaps the positions of the two parameters an and b in the original array according to the returned value.
After reading the above description, you will be very confused, you must ask where is the return value? Who is the argument to parameter a b? These are all pediatrics when you understand the following code!
/ / sort internal writing let Arr = [56, 21, 29, 105,45] Arr.sort (function (a, b) {/ / callback if (a > b) {/ a b is 56 21 return 1 / / return positive number in Arr respectively, b is arranged before a} else {return-1 / / returns negative number An is arranged before b}}) console.log (Arr) / / [21, 29, 45, 55, 105]
Execution logic:
It should be noted that the two parameters received by callback (a, b) are a = > current item and the next item of b current item. If the position of current item and next item remains the same, b is the next index-1. The condition for judging the end of the traversal is that the b parameter ends without a value. For example, in the above code, the index of the current item in the second execution of the third round is 3, then b is the next item, that is, there is no item 4 in the 4 array, and the condition for continuing traversal is not satisfied. end traversal!
Talk about the return value: the return value of 1 and-1 written in the above code is only a symbolic representation of 1 is positive-1 is negative, no matter what the return value is written in your code, sort will only judge whether your return value is positive or negative, even if the equation is established, return 100 is not valid and return-10000 is feasible.
Explain the abbreviation:
/ / abbreviated final version let Arr = [56, 21, 88, 10, 5, 77] Arr.sort ((a, b) = > a-b) / / the arrow function does not enlarge parentheses to point to the return value of this function, and the return keyword console.log (Arr) / / [5, 10, 21, 56, 77, 88]
As you can see from the figure above, the internal processing of the callback function is a-b, rather than comparing two numbers. This is because this step of comparing two numbers is done by sort. You only need to specify the return value, which happens to be mathematically defined as large-decimal = positive and decimal-large = negative.
For example, if 56-21 = 35 is a positive number, the return value is a positive number, and a positive number represents a change of position.
If 21-88 = 35 is negative, the return value is negative, and a negative number represents a change of position.
If large numbers-decimal ≠ positive numbers, decimal-large ≠ negative numbers in mathematics, it can not be so abbreviated. So it should be clear that what is done within sort is to compare with each other, not to subtract from each other.
Thank you for reading, the above is the content of "how to sort arrays in es6". After the study of this article, I believe you have a deeper understanding of how to sort arrays in es6, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.