当前位置:网站首页>笔试题“将版本号从大到小排列”
笔试题“将版本号从大到小排列”
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',
])
边栏推荐
- laravel 8 实现 订单表按月份水平分表
- Tutorial on building open source Internet of things platform
- Leetcode (142) - circular linked list II
- 2022春夏系列 KOREANO ESSENTIAL重塑时装生命力
- Cdga | what is the core of digital transformation in the transportation industry?
- P4769-[NOI2018]冒泡排序【组合数学,树状数组】
- MYSQL虚拟列
- Carbon emission reduction of second-hand trading platform, with assessment standards
- 今年的网络安全“体检”你做了吗?
- uni-app获取当前页面路由url
猜你喜欢

How to recite words in tables
![[most complete] download and installation of various versions of PS and tutorial of small test ox knife (Photoshop CS3 ~ ~ Photoshop 2022)](/img/6d/4d8d90dd221de697f4c2ab5dcc7f96.png)
[most complete] download and installation of various versions of PS and tutorial of small test ox knife (Photoshop CS3 ~ ~ Photoshop 2022)

2022第六季完美童模 海口赛区 选拔赛圆满落幕

hostname -f与uname -n的返回值可能不同

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

Wallpaper applet source code double ended wechat Tiktok applet

Transformer details

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

Mqtt second session -- emqx high availability cluster implementation

Core development board & debugger
随机推荐
手写 redux-thunk
2022年7月(软考高级)信息系统项目管理师认证招生简章
Déclaration de la variable Typescript - - assertion de type
【最全】PS各个版本下载安装及小试牛刀教程(PhotoShop CS3 ~~ PhotoShop 2022)
July 2022 (advanced soft test) information system project manager certification enrollment Brochure
hostname -f与uname -n的返回值可能不同
首次触电,原来你是这样的龙蜥社区 | 龙蜥开发者说第8期
Verilog 拼接操作符号
Verilog 大小端以及 +:使用
2022 spring summer collection koreano essential reshapes the vitality of fashion
MQTT第二话 -- emqx高可用集群实现
Typescript variable declaration - type assertion
How to recite words in tables
开发小技巧-图片资源管理
cokkie和session的区别
Product manager certification enrollment brochure (NPDP) in July 2022
一般乘法器设计,verilog code
Open3D 隐藏点移除
Wallpaper applet source code double ended wechat Tiktok applet
Verilog shift operator