当前位置:网站首页>js的sort()函数
js的sort()函数
2022-06-25 04:00:00 【刘家奕_】
sort( )
-可以用来对数组中的元素进行排序
-也会影响原数组,默认会按照Unicode编码进行排序
即使对于纯数字的数组,使用sort()排序时,也会按照Unicode编码来排序,
所以对数字进排序时,可能会得到错误的结果。
我们可以自己来指定排序的规则
我们可以在sort()添加一个回调函数,来指定排序规则,回调函数中需要定义两个形参,
浏览器将会分别使用数组中的元素作为实参去调用回调函数使用哪个元素调用不确定,但是肯定的是在数组中a一定在b前边
-浏览器会根据回调函数的返回值来决定元素的顺序,
如果返回一个大于0的值,则元素会交换位置
如果返回一个小于0的值,则元素位置不变
如果返回一个日,则认为两个元素相等,也不交换位置
例如:
<!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) {
//前边的大
if (a > b) {
return 1;
} else if (a < b) {
return -1;
} else {
return 0;
}
});
console.log(arr);
</script>
</body>
</html>

边栏推荐
- 论文阅读《LSD-SLAM: Large-Scale Direct Monocular SLAM》
- The yii2 debug toolbar is missing
- CTF_ Web: deserialization of learning notes (II) CTF classic test questions from shallow to deep
- CTF_ Web: advanced problem WP (5-8) of attack and defense world expert zone
- 1、项目第二阶段——用户注册和登陆
- "Renaissance" in the digital age? The bottom digital collection makes people happy and sad
- Intel 13th generation core showed its true colors for the first time: 68mb cache improved significantly
- IntStream API介绍
- CTF_ Web: Advanced questions of attack and defense world expert zone WP (9-14)
- How much do you know about the use value of WMS warehouse management system
猜你喜欢

mongodb集群

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

How much do you know about the use value of WMS warehouse management system

How to draw an industry investment map

Nodejs connects to MySQL through heidisql, and ER appears_ BAD_ DB_ ERROR: Unknown database 'my_ db_ books'

Finereport displays and hides column data according to conditions

"How to carry out industrial positioning" in local / Park industrial planning

1. Phase II of the project - user registration and login

冰冰学习笔记:循环队列的实现

CTF_ Web: Learn flask template injection (SSTI) from 0
随机推荐
GbASE 8s中的Blob 页(Blobspace page)
JS arrow function
"How to carry out industrial positioning" in local / Park industrial planning
IntStream API介绍
GBASE 8s存储过程语法结构
A detailed summary of TCP connection triple handshake
navicat可不可以直接操作安卓数据库SQLite
GBASE 8s的数据导入和导出
GBASE 8s的并行操作问题场景描述
L'épée leetcode fait référence au chemin leetcode de l'offre II 091 pour peindre la maison [planification dynamique] heroding
A detailed summary of four handshakes (or four waves) over TCP connections
LabVIEW开发气体调节器
小白学习MySQL - 统计的'投机取巧'
LeetCode 劍指Offer II 091 粉刷房子[動態規劃] HERODING的LeetCode之路
彻底理解数据库事务
Leetcode points to the leetcode road of offering II 091 house painting [dynamic planning] heroding
Text keyword extraction: ansj
How to screen out words related to products and eliminate invalid words accurately
ThinkPHP is integrated with esaywechat. What's wrong with wechat payment callback without callback?
Summary of various problems encountered by cocos2d-x