Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the sort () method in JavaScript

Shulou Source: shulou.com Published: 2022-06-01 03:07:01 09月22日 Update

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!

Tags: Sort from small to big small articles methods elements content size that is order not much that is parameters meaning most letters characters numbers arrays more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology vpn Shulou Information Apple OPPO Reno