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 javascript sort () sort

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

What this article shares to you is about how to use javascript sort () sorting. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Preface

There is a sort () method in Javascript array sorting, and the sort () method can be divided into two types, one is literal array sorting, the other is numeric array sorting. The following article is mainly to share with you about the recent study of javascript sort () ranking found a little understanding, the following words do not say much, let's take a look at the detailed introduction.

The principle of sort () sorting

Recently, we have ignored the principle of sort sorting before we encountered a sorting problem while doing exercises in leetcode. Let's take a look at W3C's description of sort ():

If the method is called without arguments, the elements in the array are sorted alphabetically or, more precisely, in character-encoded order.

To achieve this, you should first convert the elements of the array to strings (if necessary) for comparison.

This can explain why the ascending order of [0meme 1rem 5je 10je 8] is [0rem 1re 10je 5je 8].

The use of sort () sort parameter

W3C's use of parameters is also explained in the following paragraph: if you want to sort by other criteria, you need to provide a comparison function that compares two values and then returns a number that describes the relative order of the two values. The comparison function should have two parameters an and b, and the return values are as follows:

If an is less than b and a should appear before b in the sorted array, a value less than 0 is returned.

If an equals b, 0 is returned.

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

Therefore, it is concluded that if you want to sort in ascending order, you can achieve sort ((amaine b) = > a murb) through amurb, and reverse order can also be achieved through this:

The above is how to use javascript sort () sorting. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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.

Share To

Development

Wechat

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

12
Report