Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
版本比较
var versions= [{"version":"1.3.0-beta.2"},{"version":"1.3.0-beta.19"},{"version":"1.3.0-beta.18"},{"version":"1.3.0-beta.17"},{"version":"1.3.0-beta.16"},{"version":"1.3.0-beta.15"},{"version":"1.3.0-beta.14"},{"version":"1.3.0-beta.13"},{"version":"1.3.0-beta.12"},{"version":"1.3.0-beta.11"},{"version":"1.3.0-beta.10"},{"version":"1.3.0-beta.1"},{"version":"1.3.0"},{"version":"1.2.9"},{"version":"1.2.8"},{"version":"1.2.7"},{"version":"1.2.6"},{"version":"1.2.5"},{"version":"1.2.4"},{"version":"1.2.30"},{"version":"1.2.3"},{"version":"1.2.29"},{"version":"1.2.28"},{"version":"1.2.27"},{"version":"1.2.26"},{"version":"1.2.25"},{"version":"1.2.24"},{"version":"1.2.23"},{"version":"1.2.22"},{"version":"1.2.21"},{"version":"1.2.20"},{"version":"1.2.2"},{"version":"1.2.19"},{"version":"1.2.18"},{"version":"1.2.17"},{"version":"1.2.16"},{"version":"1.2.15"},{"version":"1.2.14"},{"version":"1.2.13"},{"version":"1.2.12"},{"version":"1.2.11"},{"version":"1.2.10"},{"version":"1.2.1"},{"version":"1.2.0rc3"},{"version":"1.2.0rc2"},{"version":"1.2.0rc1"},{"version":"1.2.0"},{"version":"1.1.5"},{"version":"1.1.4"},{"version":"1.1.3"},{"version":"1.1.2"},{"version":"1.1.1"},{"version":"1.1.0"},{"version":"1.0.8"},{"version":"1.0.7"},{"version":"1.0.6"},{"version":"1.0.5"},{"version":"1.0.4"},{"version":"1.0.3"},{"version":"1.0.2"},{"version":"1.0.1"},{"version":"1.0.0rc9"},{"version":"1.0.0rc8"},{"version":"1.0.0rc7"},{"version":"1.0.0rc6"},{"version":"1.0.0rc5"},{"version":"1.0.0rc4"},{"version":"1.0.0rc3"},{"version":"1.0.0rc2"},{"version":"1.0.0rc12"},{"version":"1.0.0rc11"},{"version":"1.0.0rc10"},{"version":"1.0.0rc1"},{"version":"1.0.0"}];
function compareVersion(a, b) {
var p1 = a.version.match(/[a-zA-Z]+|[0-9]+/g);
var p2 = b.version.match(/[a-zA-Z]+|[0-9]+/g);
var maxLength = Math.max(p1.length, p2.length);
for(var i = 0; i < maxLength; i++ ) {
p1[i] = /^\d*$/.test(p1[i]) ? parseInt(p1[i]) : p1[i];
p2[i] = /^\d*$/.test(p2[i]) ? parseInt(p2[i]) : p2[i];
if (p1[i] === undefined || typeof p2[i] === 'string' && typeof p1[i] === 'number') return 1;
if (p2[i] === undefined || typeof p1[i] === 'string' && typeof p2[i] === 'number') return -1;
if (p1[i] > p2[i]) return -1;
if (p2[i] > p1[i]) return 1;
}
return 0;
}
var ne = versions.sort(function(a, b){
return compareVersion(a, b);
});
console.log(ne);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment