const sortVersion = (versions) => {
const getMinVersion = (versions, index = 0) => {
if (versions.length === 1) {
return versions[0]
}
let min = versions[0].split('.')[index]
let arr = []
for (const value of versions) {
const num = value.split('.')[index]
if (min === num) {
arr.push(value)
} else if (+num < +min) {
min = num
arr = [value]
}
}
return getMinVersion(arr, index + 1)
}
let index = 0
const len = versions.length
const res = []
while (index < len) {
const minVersion = getMinVersion(versions)
res.push(minVersion)
const i = versions.indexOf(minVersion)
versions.splice(i, 1)
index++
}
return res
}
function sortVersion(source) {
function splitVersion(version) {
return version.split(".");
}
function compareVersionPair(versionA, versionB) {
const [aDetail, bDetail] = [splitVersion(versionA), splitVersion(versionB)];
let length = aDetail.length;
let index = 0;
console.log(`DEBUG: start to compare version pair [${versionA}, ${versionB}]`);
while (index < length) {
const currentVersionA = Number(aDetail[index]);
const currentVersionB = Number(bDetail[index]);
console.log(`DEBUG:
currentVersionA: ${currentVersionA},
currentVersionB: ${currentVersionB}`);
if (!currentVersionB) {
console.log(`Can not found detail version B, A > B`);
return 1;
}
if (currentVersionA === currentVersionB) {
console.log(`Current version A = version B`);
index++;
continue;
}
if (currentVersionA > currentVersionB) {
console.log(`Current version A > current version B`);
return 1;
} else if (currentVersionB > currentVersionA) {
console.log(`Current version A < current version B`);
return -1;
}
}
return 0;
}
source.sort(compareVersionPair);
return source;
}
const sortedVersions = sortVersion(["1.45.0", "1.5", "6", "3.3.3.3.3.3.3"]);
console.log("sortedVersions", sortedVersions);