Place current release at the top of version dropdown in Manage Packages dialog. (#12884)

* Also improved sorting for version numbers with non-numeric components.
This commit is contained in:
Cory Rivera
2020-10-12 12:20:33 -07:00
committed by GitHub
parent f61ffae15c
commit 108891ba2e
3 changed files with 37 additions and 9 deletions

View File

@@ -144,22 +144,31 @@ export function getOSPlatformId(): string {
* @returns 1 if the first version is greater, -1 if it's less, and 0 otherwise.
*/
export function comparePackageVersions(first: string, second: string): number {
let firstVersion = first.split('.').map(numStr => Number.parseInt(numStr));
let secondVersion = second.split('.').map(numStr => Number.parseInt(numStr));
let firstVersion = first.split('.');
let secondVersion = second.split('.');
// If versions have different lengths, then append zeroes to the shorter one
if (firstVersion.length > secondVersion.length) {
let diff = firstVersion.length - secondVersion.length;
secondVersion = secondVersion.concat(new Array(diff).fill(0));
secondVersion = secondVersion.concat(new Array(diff).fill('0'));
} else if (secondVersion.length > firstVersion.length) {
let diff = secondVersion.length - firstVersion.length;
firstVersion = firstVersion.concat(new Array(diff).fill(0));
firstVersion = firstVersion.concat(new Array(diff).fill('0'));
}
for (let i = 0; i < firstVersion.length; ++i) {
if (firstVersion[i] > secondVersion[i]) {
let firstVersionNum: string | number = Number(firstVersion[i]);
let secondVersionNum: string | number = Number(secondVersion[i]);
// Fallback to string comparison if either value isn't a number
if (isNaN(firstVersionNum) || isNaN(secondVersionNum)) {
firstVersionNum = firstVersion[i];
secondVersionNum = secondVersion[i];
}
if (firstVersionNum > secondVersionNum) {
return 1;
} else if (firstVersion[i] < secondVersion[i]) {
} else if (firstVersionNum < secondVersionNum) {
return -1;
}
}