In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian for you to introduce in detail "Javascript array reordering how to achieve", detailed content, clear steps, details handled properly, I hope that this "Javascript array reordering how to achieve" article can help you solve doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.
Javascript Array reordering method
There are already two methods in the array that can be used directly to reorder: reverse () and sort (). As some readers may have guessed, the reverse () method reverses the order of the array items. Take a look at the following example:
Var values= [1, 2, 3, 4, 5]
Values.reverse ()
Alert (values); / / 5, 4, 4, 3, 2, 1.
Here the initial values and order of the array are 1, 2, 3, 4, 5. After calling the reverse () method of the array, the order of its values becomes 5, 4, 3, 2, 1. The effect of this method is quite straightforward, but it is not flexible enough, so there is the sort () method.
By default, the sort () method sorts the array items in ascending order-- that is, the smallest value comes first and the largest value comes last. To achieve sorting, the sort () method calls the toString () transformation method for each array item, and then compares the resulting strings to determine how to sort them. Even if each item in the array is a numeric value, the sort () method compares a string, as follows:
Var values= [0, 1, 5, 10, 15]
Values.sort ()
Alert (values); / / 0pm 1pm 10.15je 5
As you can see, even if there is no problem with the order of the values in the example, the sort () method changes the original order based on the results of the test string. Because the value 5 is less than 10, but when comparing strings, "10" comes before "5", so the order of the array is changed. Needless to say, this sort of sorting is not the best solution in many cases. So the sort () method can take a comparison function as an argument so that we can specify which value precedes which value.
The comparison function receives two parameters, and the Nanchang website production engineer prompts that a negative number is returned if the first parameter should be before the second parameter, 0 is returned if the two parameters are equal, and a positive number is returned if the first parameter should be after the second parameter. Here is a simple comparison function:
Function compare (valuel, value2) {
If (valuel return-1:
} else if (valuel > value2) {
Return 1:
} else {
Return 0:
}
}
This comparison function works for most data types, as long as it is passed as a parameter to the sort () method, as shown in the following example:
Var values= [O, 1, 5, 10, 15]
Alert (values); / / 0jin1, 5pd10 / 15
After passing the comparison function to the sort () method, the values still maintain the correct ascending order. Of course, you can also produce the result of a descending sort by comparing the function, as long as you exchange the value returned by the comparison function:
Function compare (valuel, value2) {
If (valuel
< value2) { }else if(valuel>Value2) {
} else {
Return O:
}
}
Var values= [0, 1, 5, 10, 15]
Values.sort (compare)
Alert (values); / / 15,10pm 5jue 1jue 0
In this modified example, the comparison function returns l if the first value should be after the second, and-1 if the first value should be before the second. . Swapping return values means higher ranking of larger values, that is, sorting the array in descending order. Of course, it's faster to use the reverse () method if you just want to reverse the original order of the array.
The engineer of Nanchang Web Design Company reminded that the return values of the reverse () and sort () methods are sorted arrays.
For a numeric type or its valueOf () method returns an object type of numeric type. You can use a simpler comparison function: this function can ① simply by subtracting the first value from the second value:
Function compare (valuel, value2) {
Return value2-valuel
}
Because the comparison function affects the sort result by returning a value that is less than zero, equal to zero, or greater than zero, the subtraction operation can handle all these cases appropriately.
After reading this, the article "how to reorder Javascript arrays" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it to understand it. If you want to know more about related articles, welcome to follow 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.
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.