当前位置:网站首页>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',
])
边栏推荐
- 深卷积神经网络时代的目标检测研究进展
- Highlight in the middle of the navigation bar at the bottom of wechat applet
- 超融合架构和传统架构有什么区别?
- Analysis of c voice endpoint detection (VAD) implementation process
- Can we trust bounding box annotations for object detection
- js轮播图观后重做(较长的完整版,可运行)
- What is the difference between hyperconverged architecture and traditional architecture?
- Open3d hidden point removal
- Verilog 拼接操作符号
- Keras to tf Vgg19 input in keras_ shape
猜你喜欢

YOLO Nano:一种高度紧凑的只看一次的卷积神经网络用于目标检测

微信小程序最新canvas2d手写签名

Summary of IO streams

微信小程序分享页面,分享到朋友圈

微信小程序自定义多项选择器

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

Handwriting Redux thunk

train_on_batch保存一下loss函数变化的图像

Wechat applet determines the file format of URL

What is hyperfusion? What is the difference with traditional architecture
随机推荐
NPM common commands
Training kernel switching using GPU
(转)MySQL: ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
Open3d hidden point removal
Verilog splicing operation symbol
Remember to customize the top navigation bar of wechat applet
微信小程序自定义多项选择器
First electric shock, so you are such a dragon lizard community | dragon lizard developer said that issue 8
Unity C # e-learning (12) -- protobuf generation protocol
微信小程序wx.navigateBack返回上一页携带参数
cmd进入虚拟机
SSD改進CFENet
Verilog expression
MT yolov6 training and testing
深卷积神经网络时代的目标检测研究进展
Verilog 数据类型
Mysql使用union all统计多张表组合总数,并分别统计各表数量
手写 redux-thunk
Working for many years, recalling life -- three years in high school
什么是超融合?与传统架构有什么区别?.