Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare software version number using js? (only number)

Here is the software version number:

"1.0", "1.0.1", "2.0", "2.0.0.1", "2.0.1" 

How can I compare this?? Assume the correct order is:

"1.0", "1.0.1", "2.0", "2.0.0.1", "2.0.1" 

The idea is simple...: Read the first digit, than, the second, after that the third.... But I can't convert the version number to float number.... You also can see the version number like this:

"1.0.0.0", "1.0.1.0", "2.0.0.0", "2.0.0.1", "2.0.1.0" 

and this is more clear to see what is the idea behind... But, how to convert it into a computer program?? Do any one have any idea on how to sorting this? Thank you.

like image 409
Tattat Avatar asked Jul 26 '11 15:07

Tattat


People also ask

How do I compare version numbers?

To compare version numbers, compare their revisions in left-to-right order. Revisions are compared using their integer value ignoring any leading zeros. This means that revisions 1 and 001 are considered equal. If a version number does not specify a revision at an index, then treat the revision as 0 .


2 Answers

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.

like image 90
Jon Avatar answered Sep 21 '22 18:09

Jon


semver

The semantic version parser used by npm.

$ npm install semver

var semver = require('semver');  semver.diff('3.4.5', '4.3.7') //'major' semver.diff('3.4.5', '3.3.7') //'minor' semver.gte('3.4.8', '3.4.7') //true semver.ltr('3.4.8', '3.4.7') //false  semver.valid('1.2.3') // '1.2.3' semver.valid('a.b.c') // null semver.clean(' =v1.2.3 ') // '1.2.3' semver.satisfies('1.2.3', '1.x || >=2.5.0 || 5.0.0 - 7.2.3') // true semver.gt('1.2.3', '9.8.7') // false semver.lt('1.2.3', '9.8.7') // true  var versions = [ '1.2.3', '3.4.5', '1.0.2' ] var max = versions.sort(semver.rcompare)[0] var min = versions.sort(semver.compare)[0] var max = semver.maxSatisfying(versions, '*') 

Semantic Versioning Link :
https://www.npmjs.com/package/semver#prerelease-identifiers

like image 29
Mohammed Akdim Avatar answered Sep 22 '22 18:09

Mohammed Akdim