当前位置:网站首页>数组按指定顺序排序
数组按指定顺序排序
2022-06-12 17:32:00 【德顺】
数组排序可以直接使用 sort() 方法,可以对数组按规律排序。
但如果指定一个没有规律的顺序进行排序呢?
同样可以使用 sort() 方法:
const data = [
{ name: '张三', code: 'zs' },
{ name: '王五', code: 'ww' },
{ name: '赵七', code: 'zq' },
];
data.sort((star, next) => {
const sortList = ['zq', 'zs', 'ww']
return sortList.indexOf(star.code) - sortList.indexOf(next.code);
});这样就可以了。
边栏推荐
- Fiddler抓包几种常用功能介绍(停止抓包、清空会话窗内容、过滤请求、解码、设置断点......)
- 多种Qt的开发方式,你选择哪种?
- [BSP video tutorial] stm32h7 video tutorial Issue 8: the last issue of the MDK theme, the new generation of debugging technologies event recorder and RTT, and using stm32cubemx to generate project tem
- qemu+gdb小节
- Learn the mitmproxy packet capturing tool from scratch
- Arm64 Stack backtrack
- ShardingJDBC 分库分表详解
- 借助SpotBugs将程序错误扼杀在摇篮中
- selenium元素定位
- Cesium parabolic equation
猜你喜欢

Cicada mother talks to rainbow couple: 1.3 billion goods a year, from e-commerce beginners to super goods anchor

D. master router setting and 401 networking

MySQL事务简介、事务隔离级别

How to win the "Olympic Games" in retail technology for jd.com, the learning tyrant of the "regular examination"?

全局锁、表锁、行锁

LCD参数解释及计算

Gerrit+2触发Jenkins任务

Guitar Pro tutorial how to set up a MIDI keyboard

A variety of Qt development methods, which do you choose?

Sizepolicy policy in layout management
随机推荐
rolabelImg的安装使用
两位新晋Committer的“升级攻略”
淘宝Native研发模式的演进与思考 | DX研发模式
406. reconstruct the queue based on height
Deep interest evolution network for click through rate prediction
价值超高的CA证书解析
(3) Golang - data type
Volcano engine held a video cloud technology force summit and released a new experience oriented video cloud product matrix
Sudo of uabntu
Swintransformer network architecture
I heard that distributed IDS cannot be incremented globally?
Tidb Hackathon 2021 - pcloud: conduct icloud pcloud team interview on the database
Qiushengchang: Practice of oppo commercial data system construction
Hangzhou AI developer meetup registration opens!
Cesium抛物线方程
Gerrit+2 triggers Jenkins task
(7) Loop statement for
LCD参数解释及计算
Tcp/ip family structure and protocol of tcp/ip series overview
The R language uses the tabpct function of the epidisplay package to generate a two-dimensional contingency table, and uses the mosaic diagram to visualize the contingency table (two-dimensional conti