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

Is the ascending order of the sort method in JS a murb?

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "is the ascending order of sort method in JS is a murb". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "is the ascending order of the sort method in JS a murb?"

When you simply use the sort method, it is sorted by bit, such as:

Var arr = [1, 22, 15, 32] arr.sort () / / [1, 22, 15, 32, 22, 4, 5]

This result obviously doesn't meet the needs of most problems, so we can change the way the sort method is sorted by passing in a callback function when calling the sort method. Put the results first, and the following two sorts are ascending and descending, respectively.

Var arr = [1,22,15,32,4,5] arr.sort ((a, b) = > {return a-b}) / / sort in ascending order [1,4,5,15,22,32] arr.sort ((a, b) = > {return b-a}) / / sort in descending order [32,22,15,5,4,1]

The format of the callback function is (a return xxx b) = > {callback}, and ab is any two numbers in the array.

When the return value is greater than 0, an is placed after b

When the return value is less than 0, an is placed before b

When the return value is equal to 0, the ab position does not change.

So why is it that returning a-b is ascending and b-an is descending?

We can give a simple example, which is similar to the classified discussion of high school mathematics.

When a > b

A-b > 0, sort result = = > b _ a (ascending order)

B-a

< 0 ,排序结果 ===>

A _ r _ b (descending)

When b > a

A-b

< 0 ,排序结果 ===>

A _ r _ b (ascending order)

B-a > 0, sort result = = > b _ a (descending)

When axib

A-b = b-a = 0, the sort result = = > remains the same

It can be seen from this that

No matter a > b or b > a meme return a murb can always get the ascending order result, while return Bmera can always get the descending order result.

In addition, return a return b-an is just a simple way to write it on the basis of understanding.

If you still don't understand, you can write it like this, and here's an ascending sort:

Because the return value is > 0, put an after, so when a > b, a positive number is returned. The same will be understood later.

Var arr = [1,22,15,32,4 return 5] arr.sort ((a, b) = > {if (a > b) return 1; else if (a < b) return-1; else return 0;}); / / [1Ling 4 return-1; else return 0;}). So far, I believe you have a better understanding of "is the ascending order of the sort method in JS?" Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report