当前位置:网站首页>笔试题“将版本号从大到小排列”
笔试题“将版本号从大到小排列”
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',
])
边栏推荐
- 51 MCU interrupt and timer counter, based on Puzhong technology hc6800-esv2.0
- 积分商城运营要如何做才能获取到利润
- io流的总结
- Core development board & debugger
- js for in循环 for of循环的区别以及用法
- 工作好多年,回忆人生--高中三年
- 闭关修炼(二十五)基础web安全
- MQTT第二话 -- emqx高可用集群实现
- CDGA|交通行业做好数字化转型的核心是什么?
- Did you really make things clear when you were promoted or reported?
猜你喜欢

对比HomeKit、米家,智汀家庭云版有哪些场景化的体验

Compare homekit, MI family, and zhiting family cloud edition for what scene based experiences

uni-app获取当前页面路由url

Leetcode (142) - circular linked list II

Robcogen tutorial of robot code generator

2022第六季完美童模 合肥赛区 决赛圆满落幕

【无标题】

Core development board & debugger

CDGA|交通行业做好数字化转型的核心是什么?

Carbon emission reduction of second-hand trading platform, with assessment standards
随机推荐
TypeScript 变量声明 —— 类型断言
Development tips - Image Resource Management
verilog 移位操作符
今年的网络安全“体检”你做了吗?
Some "non-technical" Thoughts on distributed digital identity
Enrollment brochure for system integration project management engineer certification in July 2022
Robcogen tutorial of robot code generator
2022第六季完美童模 合肥賽區 决賽圓滿落幕
Did you really make things clear when you were promoted or reported?
MT-yolov6训练及测试
MYSQL行转列例子
手写VirtualDOM
MySQL virtual column
mysql insert 时出现Deadlock死锁场景分析
C# 语音端点检测(VAD)实现过程分析
The final of the sixth season of 2022 perfect children's model Hefei division came to a successful conclusion
Verilog equivalent operator
微积分学习
宏,函数和内联函数
工作好多年,回忆人生--高中三年