Compare version numbers without using split function

How to compare version numbers?

For example:

x = 1.23.56.1487.5

y = 1.24.55.487.2

+105
string c # versioning compare version
Sep 27 '11 at 11:11
source share
5 answers

Can I use the version class?

http://msdn.microsoft.com/en-us/library/system.version.aspx

It has an IComparable interface. Remember that this will not work with a 5-part version as shown in the figure (is this really your version string?). Assuming your inputs are strings, here's a working example with a regular version of the .NET 4-part version:

static class Program { static void Main() { string v1 = "1.23.56.1487"; string v2 = "1.24.55.487"; var version1 = new Version(v1); var version2 = new Version(v2); var result = version1.CompareTo(version2); if (result > 0) Console.WriteLine("version1 is greater"); else if (result < 0) Console.WriteLine("version2 is greater"); else Console.WriteLine("versions are equal"); return; } } 
+251
Sep 27 '11 at 11:13
source share
โ€” -

If you can live using the major.minor.build.revision schema, you can use the .Net Version class. Otherwise, you will have to implement some kind of parsing from left to right and continue until you get the difference or return that the two versions are equal.

+12
Sep 27 '11 at 11:15
source share

In addition to @JohnD's answer, it may be necessary to compare only partial version numbers without using Split ('.') Or another int line feed. I just wrote a CompareTo extension method with 1 additional argument - the number of significant parts of the version number to compare (between 1 and 4).

 public static class VersionExtensions { public static int CompareTo(this Version version, Version otherVersion, int significantParts) { if(version == null) { throw new ArgumentNullException("version"); } if(otherVersion == null) { return 1; } if(version.Major != otherVersion.Major && significantParts >= 1) if(version.Major > otherVersion.Major) return 1; else return -1; if(version.Minor != otherVersion.Minor && significantParts >= 2) if(version.Minor > otherVersion.Minor) return 1; else return -1; if(version.Build != otherVersion.Build && significantParts >= 3) if(version.Build > otherVersion.Build) return 1; else return -1; if(version.Revision != otherVersion.Revision && significantParts >= 4) if(version.Revision > otherVersion.Revision) return 1; else return -1; return 0; } } 
+6
Feb 24 '15 at 12:35
source share
 public int compareVersion(string Version1,string Version2) { System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"([\d]+)"); System.Text.RegularExpressions.MatchCollection m1 = regex.Matches(Version1); System.Text.RegularExpressions.MatchCollection m2 = regex.Matches(Version2); int min = Math.Min(m1.Count,m2.Count); for(int i=0; i<min;i++) { if(Convert.ToInt32(m1[i].Value)>Convert.ToInt32(m2[i].Value)) { return 1; } if(Convert.ToInt32(m1[i].Value)<Convert.ToInt32(m2[i].Value)) { return -1; } } return 0; } 
+4
Jun 30
source share

If for some reason you are not allowed to use the Version comparison method directly (for example, in a client-server scenario), another approach is to extract a long number from the version and then compare the numbers with each other. However, the number should have the following format: two digits for Major, Minor and Revision, and four for Build.

How to extract the version number:

 var version = Assembly.GetExecutingAssembly().GetName().Version; long newVersion = version.Major * 1000000000L + version.Minor * 1000000L + version.Build * 1000L + version.Revision; 

And then somewhere else you can simply compare:

 if(newVersion > installedVersion) { //update code } 

Note: The installed version is the previously extracted long number.

+1
Oct 08 '18 at 10:10
source share



All Articles