Delve into the array sort function of JS sort () (ii)

Source: Internet
Author: User
Keywords function depth array

Intermediary transaction http://www.aliyun.com/zixun/aggregation/6858.html ">seo diagnose Taobao guest cloud host technology Hall

This is an explanation of the JS array function sort. Delve into the array sort functions in JS (sort) and reverse (). Looking at this article, I hope you will see the first article. Because this article is based on the last article.

Okay, no more nonsense. Start getting into our business today.

I feel like we're doing most of the process of web programming with strings and arrays of these two array types.

8630.html "> Sometimes, we are all sorting, especially array sort. It's possible that we're just trying to sort out the data in a set of data. As shown in the following example.

var data = [{name: "Tom", Age: "},{name"-"Cat", Age: "29"} ...];

We just want to sort the name or age in this set of data, and you'll find that using the Self generation Function sort () in JS doesn't solve this type of problem. This article is to solve this problem?

To briefly describe the problem: Suppose you have an array of objects, and we want to sort by the array of an object's attributes. The comparison function passed to the array sort () method receives two parameters, which are the values of the comparison. However, we need a way to indicate which attribute to sort by. and the sort () method can only accept a parameter, at this time, we can use the characteristics of JS custom functions to do. All right, let's see.

  

See above, you may be a little bit confused, this function definition seems a bit complicated, but in fact nothing more than the inside of this function nested another function, and, this internal function preceded by a return operator. After the internal function receives the PName argument, the value of the specified property is obtained by means of the square brackets (which can be considered array values) notation. Well, here you may not understand a bit, is our last article in the interpretation of the time, obviously sort in passing parameters is just a function without parameters. Here we can review the relevant definition of the JS function: To access the function's pointer without executing the function, you must remove the parentheses behind the function. Also, function names in ECMAScript are variables, so functions can be used as values. That is, not only can you pass a function function to another function like passing a parameter, but you can return a function as the result of another function.

Well, I'm sure everyone can now understand the difference between this article in sort and the parameters of the sort function in the previous article. Of course, this is not our point.

So, we're warming it, sort this function, when using its arguments (this function is a custom function), he will, in the specified array of two elements passed to the custom function.

Well, here's the same thing, but here's a little bit different from the previous article, which is that the array in the previous article is a one-dimensional array. And this article is a two-dimensional array, so this time, it is not difficult to think, passed into the function of the two parameters of the value, is two arrays, this time, in the internal inside we use the parameter [PName] This combination can be taken to the two functions of the specific properties of the value. This time, compare the two values in the comparison method of the sort function. is done, specifying the comparison of the functions.

All right, here we go. I hope you can see it clearly. If, what do not understand can be in the article below the message.

The beginning of the original tutorial

Related Article

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.