当前位置:网站首页>笔试题“将版本号从大到小排列”
笔试题“将版本号从大到小排列”
2022-06-29 08:26:00 【swag_特约男演员】
将版本号从大到小排列
例如 [‘1.2.3’,‘2.2.1’,‘10.1.0’,‘9.99.9’,‘10.1.0’]
输出 [‘1.2.3’,‘2.2.1’,‘9.99.9’,‘10.1.0’,‘10.1.0’]
思路
版本号怎么比较?
版本号x.y.z,先比较x,如果x相等,再比较y,如果y也相等,最后比较z。
我的想法是将每一个元素从字符串变成按照“.”分割的数组,形成二维数组,再用内置函数sort去比较。
代码

// 将版本号从大到小排列
// 例如 ['1.2.3','2.2.1','10.1.0','9.99.9','10.1.0']
// 输出 ['1.2.3','2.2.1','9.99.9','10.1.0','10.1.0']
function sortVersion(array) {
for (let i = 0; i < array.length; i++) {
array[i] = array[i].split('.')
}
array.sort((a, b) => {
return a[0] - b[0] || a[1] - b[1] || a[2] - b[2] // 先比较版本号的x,如果x相等再比较y,如果x,y都相等最后比较z。
})
for (let i = 0; i < array.length; i++) {
array[i] = array[i].join()
}
console.log(array)
}
sortVersion(['1.2.3', '2.2.1', '10.1.0', '9.99.9', '10.1.0'])
sortVersion([
'1.2.3',
'2.2.1',
'10.1.0',
'9.99.9',
'10.1.0',
'3.14.15',
'3.15.12',
'3.14.16',
])
边栏推荐
- 名企实习一年要学会的15件事,这样你就省的走弯路了。
- DevOps到底是什么意思?
- P6776-[noi2020] surreal tree
- Is it safe for the top ten securities companies to open accounts? Is it reliable?
- 通过ELO机制衡量各类对弈活动水平
- uniapp微信小程序报错 TypeError: Cannot read property ‘call‘ of undefined
- 今年的网络安全“体检”你做了吗?
- 操作系统产品密钥查看方法
- 今天让你知道PMP考试通过率达97%,可信不可信
- Is it really safe to open a stock account online? Find the answer
猜你喜欢

802.11--802.11n protocol phy

laravel 8 实现 订单表按月份水平分表

The @dynamicmemberlookup and callasfunction features in swift implement the object transparent proxy function

Déclaration de la variable Typescript - - assertion de type

General multiplier design, verilog code

Unity C# 网络学习(十二)——Protobuf生成协议

二手交易平台碳减排,有了评估标准

闭关修炼(二十)如何做好单元测试

首次触电,原来你是这样的龙蜥社区 | 龙蜥开发者说第8期

手写 redux-thunk
随机推荐
批量处理实验接触角数据-MATLAB分析
io流的总结
51 MCU interrupt and timer counter, based on Puzhong technology hc6800-esv2.0
闭关修炼(二十一)Servlet生命周期、service方法源码分析、线程安全问题
cokkie和session的区别
2022第六季完美童模 海口赛区 选拔赛圆满落幕
mysql insert 时出现Deadlock死锁场景分析
首次触电,原来你是这样的龙蜥社区 | 龙蜥开发者说第8期
宏,函数和内联函数
Verilog reduction operator
The final of the sixth season of 2022 perfect children's model Hefei division came to a successful conclusion
2022第六季完美童模 合肥賽區 决賽圓滿落幕
npm常用命令
背包九讲——全篇详细理解与代码实现
802.11--802.11n protocol phy
闭关修炼(二十)如何做好单元测试
Let you know today that the passing rate of the PMP Exam is 97%, is it believable
How to recite words in tables
Cdga | what is the core of digital transformation in the transportation industry?
H5软键盘问题