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 the sort () method in JavaScript

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

Share

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

Editor to share with you how to use the sort () method in JavaScript, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Sort ()

If srot () takes no arguments, it sorts the elements in the array alphabetically, that is, in the order in which the characters are encoded.

If the number type is not sorted normally from smallest to largest, it is more like sorting by the size of the first digit of the element.

If you want to sort from big to small, from small to big. This can be achieved by comparing the size of the value.

From small to large, the meaning of a murb: a murb is greater than 0, which means that an is large, then put an after. If a color b is less than 0, it means that b is large, then b is later. If amurb equals 0, it remains the same. From big to small is the opposite of the above, do not say (do not want to write, the virtue of programmers is lazy)

As for why it is a-b _ r _ r _ b-a, you can read this article https://www.yisu.com/article/238275.htm

Var arr1 = ['asides,' dudes, 'canals,' b']; arr.sort (); / ['asides,' bads, 'cations,' d'] var arr2 = [10,5,40,25,100,1]; arr2.sort (); / / [1,10,100,25,40,5] / sort from small to large var arr2 = [10,5,40,25,100,1] Arr2.sort ((amemb) = > amerb) console.log (arr2); / / [1,5,10,25,40,100] / / sort from large to small var arr2 = [10,5,40,25,100,1]; arr2.sort ((amemb) = > BMura) console.log (arr2) / [100,40,25,10,5,1] above are all the contents of this article entitled "how to use the sort () method in JavaScript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.

Share To

Development

Wechat

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

12
Report