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

边栏推荐
- A detailed summary of TCP connection triple handshake
- 计算学生成绩等级(虚函数和多态)
- PHP encapsulates curl to send get and post request methods, and uses
- Simple text analysis of malicious samples - Introduction
- 微信小程序父子组件之间传值
- Sourcetree pulls the code and prompts to fill in authentic, but the configuration cannot change the user
- Part I Verilog quick start
- GBASE 8s存儲過程語法結構
- Communication problems in parent and child components of uniapp
- CTF_ Web: Learn flask template injection (SSTI) from 0
猜你喜欢

CTF_ Web: advanced problem WP (5-8) of attack and defense world expert zone

Intel 13th generation core showed its true colors for the first time: 68mb cache improved significantly
![[esp32 learning path 6 - Flash encryption]](/img/4c/f317ca4823dca50a9bccd285967ab0.png)
[esp32 learning path 6 - Flash encryption]

i. Max development board learning record

简单的恶意样本行文分析-入门篇

Anaconda installation +tensorflow installation +keras installation +numpy installation (including image and version information compatibility issues)

Anaconda安装+TensorFlow安装+Keras安装+numpy安装(包含镜像和版本信息兼容问题)

Record small knowledge points

LabVIEW development gas regulator

Finereport displays and hides column data according to conditions
随机推荐
简单的恶意样本行文分析-入门篇
JS arrow function
Gbase 8s parallel operation problem scenario description
GBASE 8s的数据导入和导出
sql_ mode=only_ full_ group_ By's pit
Should I use on or where for the left join
Gbase 8s stored procedure syntax structure
Trigger for gbase 8s
ThinkPHP is integrated with esaywechat. What's wrong with wechat payment callback without callback?
为什么TCP握手刚刚好是3次呢?
Basic introduction of gbase 8s blocking technology
GBASE 8s 总体架构
CTF_ Web: Advanced questions of attack and defense world expert zone WP (9-14)
单元测试覆盖率
Thorough understanding of database transactions
CTF_ Web: Advanced questions of attack and defense world expert zone WP (1-4)
Comparison of towe/ JIRA / tapd / Zen collaboration platforms
深度学习——几种学习类型
关于TCP连接四次握手(或者叫四次挥手)的详细总结
CTF_ Web: deserialization learning notes (I) classes and objects in PHP