当前位置:网站首页>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>

边栏推荐
- Simple text analysis of malicious samples - Introduction
- 使用文本分析识别一段文本中的主要性别
- Laravel document sorting 9. Blade template
- Gbase 8s stored procedure execution and deletion
- Sourcetree pulls the code and prompts to fill in authentic, but the configuration cannot change the user
- CTF_ Web: advanced problem WP (5-8) of attack and defense world expert zone
- [esp32 learning path 6 - Flash encryption]
- Introduction to the isolation level of gbase 8s
- The yii2 debug toolbar is missing
- kenlm
猜你喜欢
![[esp32 learning path 6 - Flash encryption]](/img/4c/f317ca4823dca50a9bccd285967ab0.png)
[esp32 learning path 6 - Flash encryption]

微信小程序父子组件之间传值

Coinlist how to operate the middle lot number security tutorial

Paper notes: multi label learning ESMC (I don't understand it, but I haven't written it yet, so I'll put it here for a place temporarily)

单元测试覆盖率

Easyrecovery15 very easy to use computer data recovery software

A detailed summary of TCP connection triple handshake

Part I Verilog quick start

CTF_ Web: Advanced questions of attack and defense world expert zone WP (15-18)

《牛客刷verilog》Part I Verilog快速入门
随机推荐
php开发支付宝支付功能之扫码支付流程图
BSC smart contract dividend mainstream currency | including marketing wallet | deflation | reflow | dividend free token | available for direct deployment
Le chemin de l'apprentissage immutable - - Adieu à la copie traditionnelle
Nodejs connects to MySQL through heidisql, and ER appears_ BAD_ DB_ ERROR: Unknown database 'my_ db_ books'
A detailed summary of TCP connection triple handshake
JS arrow function
Comparison of towe/ JIRA / tapd / Zen collaboration platforms
unity Quad剔除背面并剔除透明部分的shader
PostgreSQL数据库WAL——RM_HEAP_ID日志记录动作
CTF_ Web: Advanced questions of attack and defense world expert zone WP (15-18)
GBASE 8s 索引R树
CTF_ Web: Advanced questions of attack and defense world expert zone WP (19-21)
Laravel document sorting 7. View
EasyRecovery15非常好用的电脑数据恢复软件
Sourcetree pulls the code and prompts to fill in authentic, but the configuration cannot change the user
js的arguments
LabVIEW开发气体调节器
Gbase 8s stored procedure syntax structure
什么是存储引擎以及MySQL常见的三种数据库存储引擎
CTF_ Web: how to recognize and evaluate a regular expression