当前位置:网站首页>JS' sort() function
JS' sort() function
2022-06-25 04:36:00 【Liujiayi_】
sort( )
- It can be used to sort the elements in the array
- It will also affect the original array , By default, it will follow Unicode Code to sort
Even for arrays of pure numbers , Use sort() Sorting time , I will also follow Unicode Code to sort ,
So when sorting numbers , You may get the wrong result .
We can specify the sorting rules by ourselves
We can do it in sort() Add a callback function , To specify the collation , Two formal parameters need to be defined in the callback function ,
The browser will use the elements in the array as arguments to call the callback function, and it is uncertain which element to call , But it's definitely in the array a It must be b in front
- The browser will determine the order of elements according to the return value of the callback function ,
If it returns a value greater than 0 Value , Then the element will exchange positions
If it returns a value less than 0 Value , Then the element position remains unchanged
If a day is returned , The two elements are considered equal , And don't exchange positions
for example :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
var arr = [2, 4, 6, 7];
arr.sort(function (a, b) {
// Big in front
if (a > b) {
return 1;
} else if (a < b) {
return -1;
} else {
return 0;
}
});
console.log(arr);
</script>
</body>
</html>
边栏推荐
- cnpm : 无法加载文件 C:\Users\Administrator\AppData\Roaming\npm\cnpm.ps1,因为在此系统上禁止运行脚本。
- What is persistence? What are RDB and AOF in redis persistence?
- 写shell脚本报错总结
- Introduction to intstream API
- 我的IC之旅——资深芯片设计验证工程师成长——“胡”说IC工程师完美进阶
- Nodejs connects to MySQL through heidisql, and ER appears_ BAD_ DB_ ERROR: Unknown database 'my_ db_ books'
- Gbase 8s stored procedure execution and deletion
- GBASE 8s的隔离级别介绍
- js中的concat()
- Retrofit source code analysis
猜你喜欢
机器学习深度学习——向量化
Coinlist how to operate the middle lot number security tutorial
Finereport (sail soft) handling the problem that the histogram data label is blocked
Vscode 设置clang-format
PHP extracts and analyzes table contents, and collects bidding information
php开发支付宝支付功能之扫码支付流程图
jsz中的join()
Cnpm: unable to load file c:\users\administrator\appdata\roaming\npm\cnpm PS1 because running scripts is prohibited on this system.
CTF_ Web: basic 12 questions WP of attack and defense world novice zone
三角形类(构造与析构)
随机推荐
Synchronous and asynchronous functions (callback function, promise, generator, async/await)
2020.3.3 notes async/await and promise and Then processes and threads
Coinlist queuing tutorial to improve the winning rate
JS arrow function
GBASE 8s存储过程执行和删除
使用文本分析识别一段文本中的主要性别
什么是数据持久化?
关于TCP连接三次握手的详细总结
Comparison of towe/ JIRA / tapd / Zen collaboration platforms
GBASE 8s活锁、死锁问题的解决
How to screen out words related to products and eliminate invalid words accurately
Laravel document sorting 8. Middleware
什么是持久化?redis 持久化中的RDB和AOF是什么?
i. Max development board learning record
Gbase 8s index R tree
Laravel document sorting 4. Controller
halcon之区域:多种区域(Region)生成(3)
CTF_ Web:8-bit controllable character getshell
Smart contract learning materials
CTF_ Web: Advanced questions of attack and defense world expert zone WP (15-18)