quality_frontend/packages/common/javascript/sort.js

114 lines
2.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @description: 输入需要排序的两个参数返回index配合sort函数使用
* @param {Date | milliseconds} 必选
* @return: {Date} 返回0时0分0秒的Date
*/
export function getChartType (char) {
// 数字可按照排序的要求进行自定义,我这边产品的要求是
// 数字0->9->大写字母A->Z->小写字母a->z->中文拼音a->z
if (/^[\u4e00-\u9fa5]$/.test(char)) {
return ['zh', 300]
}
if (/^[a-zA-Z]$/.test(char)) {
return ['en', 200]
}
if (/^[0-9]$/.test(char)) {
return ['number', 100]
}
return ['others', 999]
}
export function sortDevName (str1, str2) { // 混合排序
let res = 0
for (let i = 0; ;i += 1) {
if (!str1[i] || !str2[i]) {
res = str1.length - str2.length
break
}
const char1 = str1[i]
const char1Type = getChartType(char1)
const char2 = str2[i]
const char2Type = getChartType(char2) // 类型相同的逐个比较字符
if (char1Type[0] === char2Type[0]) {
if (char1 === char2) {
break
}
if (char1Type[0] === 'zh') {
res = char1.localeCompare(char2)
} else if (char1Type[0] === 'en') {
res = char1.charCodeAt(0) - char2.charCodeAt(0)
} else {
res = char1 - char2
}
break
} else { // 类型不同的,直接用返回的数字相减
res = char1Type[1] - char2Type[1]
break
}
}
return res
}
/**
* @description: 输入一个数组,'版本号'
* @param {Array} 必选
* @return: {Array} 返回排序后的数组
*/
export function sortVersion (arr, Isreverse = false) {
let newList = arr.sort((a, b) => {
let i = 0
const arr1 = a.split('.')
const arr2 = b.split('.')
while (true) {
const s1 = arr1[i]
const s2 = arr2[i++]
if (s1 === undefined || s2 === undefined) {
return arr2.length - arr1.length
}
if (s1 === s2) continue
return s2 - s1
}
})
return newList
}
/**
* @description: 输入一个'版本号'
* @param {String} 必选
* @return: {String} 返回版本号的下一个版本
*/
export function supgradeVersion (str) {
let newstr = ''
let arr = str.split('.').map(ele => Number(ele)).reverse()
arr.forEach((ele, index) => {
switch (index) {
case 0:
if (ele === 9) {
arr[index] = 0
arr[index + 1] = arr[index + 1] + 1
} else {
arr[index] = ele + 1
}
break
case arr.length:
arr[index] = ele + 1
break
default:
if (ele > 9) {
arr[index] = 0
arr[index + 1] = arr[index + 1] + 1
} else {
arr[index] = ele
}
break
}
})
newstr = arr.reverse().join('.')
return newstr
}