How to Compare Software Version Number Using JS? (Only Number)

How can I compare software version number using JavaScript? (only numbers)

The basic idea to make this comparison would be to use Array.split to get arrays of parts from the input strings and then compare pairs of parts from the two arrays; if the parts are not equal we know which version is smaller.

There are a few of important details to keep in mind:

  1. How should the parts in each pair be compared? The question wants to compare numerically, but what if we have version strings that are not made up of just digits (e.g. "1.0a")?
  2. What should happen if one version string has more parts than the other? Most likely "1.0" should be considered less than "1.0.1", but what about "1.0.0"?

Here's the code for an implementation that you can use directly (gist with documentation):

function versionCompare(v1, v2, options) {
var lexicographical = options && options.lexicographical,
zeroExtend = options && options.zeroExtend,
v1parts = v1.split('.'),
v2parts = v2.split('.');

function isValidPart(x) {
return (lexicographical ? /^\d+[A-Za-z]*$/ : /^\d+$/).test(x);
}

if (!v1parts.every(isValidPart) || !v2parts.every(isValidPart)) {
return NaN;
}

if (zeroExtend) {
while (v1parts.length < v2parts.length) v1parts.push("0");
while (v2parts.length < v1parts.length) v2parts.push("0");
}

if (!lexicographical) {
v1parts = v1parts.map(Number);
v2parts = v2parts.map(Number);
}

for (var i = 0; i < v1parts.length; ++i) {
if (v2parts.length == i) {
return 1;
}

if (v1parts[i] == v2parts[i]) {
continue;
}
else if (v1parts[i] > v2parts[i]) {
return 1;
}
else {
return -1;
}
}

if (v1parts.length != v2parts.length) {
return -1;
}

return 0;
}

This version compares parts naturally, does not accept character suffixes and considers "1.7" to be smaller than "1.7.0". The comparison mode can be changed to lexicographical and shorter version strings can be automatically zero-padded using the optional third argument.

There is a JSFiddle that runs "unit tests" here; it is a slightly expanded version of ripper234's work (thank you).

Important note: This code uses Array.map and Array.every, which means that it will not run in IE versions earlier than 9. If you need to support those you will have to provide polyfills for the missing methods.

How can I compare software version number using JavaScript? (only numbers)

The basic idea to make this comparison would be to use Array.split to get arrays of parts from the input strings and then compare pairs of parts from the two arrays; if the parts are not equal we know which version is smaller.

There are a few of important details to keep in mind:

  1. How should the parts in each pair be compared? The question wants to compare numerically, but what if we have version strings that are not made up of just digits (e.g. "1.0a")?
  2. What should happen if one version string has more parts than the other? Most likely "1.0" should be considered less than "1.0.1", but what about "1.0.0"?

Here's the code for an implementation that you can use directly (gist with documentation):

function versionCompare(v1, v2, options) {
var lexicographical = options && options.lexicographical,
zeroExtend = options && options.zeroExtend,
v1parts = v1.split('.'),
v2parts = v2.split('.');

function isValidPart(x) {
return (lexicographical ? /^\d+[A-Za-z]*$/ : /^\d+$/).test(x);
}

if (!v1parts.every(isValidPart) || !v2parts.every(isValidPart)) {
return NaN;
}

if (zeroExtend) {
while (v1parts.length < v2parts.length) v1parts.push("0");
while (v2parts.length < v1parts.length) v2parts.push("0");
}

if (!lexicographical) {
v1parts = v1parts.map(Number);
v2parts = v2parts.map(Number);
}

for (var i = 0; i < v1parts.length; ++i) {
if (v2parts.length == i) {
return 1;
}

if (v1parts[i] == v2parts[i]) {
continue;
}
else if (v1parts[i] > v2parts[i]) {
return 1;
}
else {
return -1;
}
}

if (v1parts.length != v2parts.length) {
return -1;
}

return 0;
}

This version compares parts naturally, does not accept character suffixes and considers "1.7" to be smaller than "1.7.0". The comparison mode can be changed to lexicographical and shorter version strings can be automatically zero-padded using the optional third argument.

There is a JSFiddle that runs "unit tests" here; it is a slightly expanded version of ripper234's work (thank you).

Important note: This code uses Array.map and Array.every, which means that it will not run in IE versions earlier than 9. If you need to support those you will have to provide polyfills for the missing methods.

How can I compare arbitrary version numbers?

function cmpVersion(a, b) {
var i, cmp, len;
a = (a + '').split('.');
b = (b + '').split('.');
len = Math.max(a.length, b.length);
for( i = 0; i < len; i++ ) {
if( a[i] === undefined ) {
a[i] = '0';
}
if( b[i] === undefined ) {
b[i] = '0';
}
cmp = parseInt(a[i], 10) - parseInt(b[i], 10);
if( cmp !== 0 ) {
return (cmp < 0 ? -1 : 1);
}
}
return 0;
}

function gteVersion(a, b) {
return cmpVersion(a, b) >= 0;
}
function ltVersion(a, b) {
return cmpVersion(a, b) < 0;
}

This function handles:

  • numbers or strings as input
  • trailing zeros (e.g. cmpVersion("1.0", 1) returns 0)
  • ignores trailing alpha, b, pre4, etc

Javascript function to compare two versions

This should work:

function compareversion(version1,version2){

var result=false;

if(typeof version1!=='object'){ version1=version1.toString().split('.'); }
if(typeof version2!=='object'){ version2=version2.toString().split('.'); }

for(var i=0;i<(Math.max(version1.length,version2.length));i++){

if(version1[i]==undefined){ version1[i]=0; }
if(version2[i]==undefined){ version2[i]=0; }

if(Number(version1[i])<Number(version2[i])){
result=true;
break;
}
if(version1[i]!=version2[i]){
break;
}
}
return(result);
}

The reason compareversion('1.1.0','1.0.1') fails is that your code first compares 1 to 1, then 1 to 0 (it does not break here since it only breaks if version1[i] < version2[i]) and then 0 to 1.

Since 0 < 1, it returns false.

How to pass a version number as an integer in JavaScript

As pointed by comments, 4.0.2 is not an integer...

Anyway, following this: How to do version numbers?, version numbers are usually more that a simple integer.

What is usually done is to have a structure to hold version number:

var currentVersion = {
major : 4,
minor: 0,
release: 2,
// build : undefined
};

This can be built from a string "4.0.2" using a split function:

(function(outputVersion, inputString) {
"use strict";

var splitted = myString.split('.');
outputVersion.major = splitted.length > 0 ? splitted[0]: undefined;
outputVersion.minor = splitted.length > 1 ? splitted[1]: undefined;
// so on for other properties
}(window.applicationVersion, myString));

Please note that above code is just a sample, not the way it should be done, it all depends on your application code. In particular, you might want to compare versions together, which means you will need comparison functions based on lexicographic order: How to compare software version number using js? (only number)



Related Topics



Leave a reply



Submit