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 does js sort array elements

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

Share

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

This article introduces the knowledge of "how to sort array elements by js". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Using the for loop, you can choose bubbling sort.

/ / compare the size of the two adjacent numbers in turn in each round. If the back is smaller than the front, exchange var baccarat / set the value used to change positions var a = [1, var 9, 3, 2, 5, 5, 34, 23, 98, 14] / / Bubble sort for (var ionomy 0 umbelli for (var jung0) J if (a [j] > a [juni1]) {bachela [j] a [j] = a [j + 1] a [juni1] = b} console.log (a)

2. The sort () method can sort array elements according to certain conditions. It is divided into two cases: the method parameter is empty and the function is empty.

When the parameter of the method is empty, the elements in the array are sorted alphabetically from smallest to largest.

Var a = a.sort (); / / sort elements in alphabetical order console.log (a)

When the argument is a function, the array elements are sorted according to the rules specified by the function.

Var a = [1 return, 2, 40, 40, 50, 5, 50, 6, 7] a.sort (function (Arecedence b) {return b) {order from small to big / / return b from big to small}); console.log (a); this is the end of the content of "how to sort array elements by js". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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