当前位置:网站首页>Written test question "arrange version numbers from large to small"
Written test question "arrange version numbers from large to small"
2022-06-29 09:18:00 【swag_ Special actor】
Arrange the version numbers from large to small
for example [‘1.2.3’,‘2.2.1’,‘10.1.0’,‘9.99.9’,‘10.1.0’]
Output [‘1.2.3’,‘2.2.1’,‘9.99.9’,‘10.1.0’,‘10.1.0’]
Ideas
How to compare version numbers ?
Version number x.y.z, Compare first x, If x equal , Compare again y, If y Also equal , Last comparison z.
My idea is to change each element from a string to a string “.” Split array , Form a two-dimensional array , Using built-in functions sort To compare .
Code

// Arrange the version numbers from large to small
// for example ['1.2.3','2.2.1','10.1.0','9.99.9','10.1.0']
// Output ['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] // Compare the version numbers first x, If x Equal recomparison y, If x,y All equal final comparison 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',
])
边栏推荐
猜你喜欢
随机推荐
npm常用命令
[to.Net] C data model, from Entity Framework core to LINQ
Universal target detection based on region attention
mysql insert 时出现Deadlock死锁场景分析
Verilog 拼接操作符号
微信小程序项目:微信小程序页面布局
Detailed version of two-stage target detection principle
Remember to customize the top navigation bar of wechat applet
MYSQL虚拟列
Which securities company is good for opening a mobile account? Is it safe to open an account online?
微信小程序搜索关键字高亮和ctrl+f搜索定位实现
Unity C# 网络学习(十二)——Protobuf生成协议
Abstract classes and interfaces
基于区域注意的通用目标检测
js轮播图观后重做(较长的完整版,可运行)
MYSQL行转列例子
微信小程序最新canvas2d手写签名
Let's make a summary
训练查看(问题暂存)
深卷积神经网络时代的目标检测研究进展








