We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
versions是一个项目的版本号列表,因多人维护,不规则 var versions=['1.45.0','1.5','6','3.3.3.3.3.3.3'] 要求从小到大排序,注意'1.45'比'1.5'大 。var sorted=['1.5','1.45.0','3.3.3.3.3.3','6']
The text was updated successfully, but these errors were encountered:
function versionSort(arr) { //1. // 先将版本号每一位放到数组保存 var temp = arr.map(v => v.split(".")) //利用sort属性排序传入规则函数 function rule(a, b) { var j = 0 var minLen = Math.min(a.length, b.length) while (j < minLen) { if (parseInt(a[j]) > parseInt(b[j])) { return 1 } else if (parseInt(a[j]) < parseInt(b[j])) { return -1 } j++ } // 遇到1.5与1.5.5的情况上面无法判断 if (minLen == a.length) { return -1 } else if (minLen == b.length) { return 1 } } // 将比较后的版本从数组形式转为.的形式 return temp.sort(rule).map(v => v.join(".")) //2.同样的道理,写法更简单 function rule(a, b) { var arr1 = a.split(".") var arr2 = b.split(".") var i = 0 while (true) { var s1 = arr1[i], s2 = arr2[i++] if (s1 === undefined || s2 === undefined) return arr1.length - arr2.length //1.5与1.5.5的情况 if (s1 === s2) continue //相等就继续循环 return s1 - s2 } } return arr.sort(rule) }
Sorry, something went wrong.
遇到两次
No branches or pull requests
versions是一个项目的版本号列表,因多人维护,不规则 var versions=['1.45.0','1.5','6','3.3.3.3.3.3.3'] 要求从小到大排序,注意'1.45'比'1.5'大 。var sorted=['1.5','1.45.0','3.3.3.3.3.3','6']
The text was updated successfully, but these errors were encountered: