How to Compare Version Number String in C#?


In software engineering, the version number usually comprises of 4 integers separated by dots. For example, 1.0.0.2.

Version comparison is usually carried out from left most to the right most numbers. For example, 1.2.3.4 is a bigger version than 1.1.9.7.

We can use the string type to represent a version. And to compare it, we need to split the version string into group of digits, and comparing from the left to the right is the way to find out which version is bigger.

The following C# function test if the first version is bigger than the second version. The this keyword is specified for the first parameter, so you can use v1.IsVersionLater(v2) given the fact that the function is marked static.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
        public static bool IsVersionLater(this string v1, string v2)
        {
            // split into groups
            string[] cur = v1.Split('.');
            string[] cmp = v2.Split('.');
            // get max length and fill the shorter one with zeros
            int len = Math.Max(cur.Length, cmp.Length);
            int[] vs = new int[len];
            int[] cs = new int[len];
            Array.Clear(vs, 0, len);
            Array.Clear(cs, 0, len);
            int idx = 0;
            // skip non digits
            foreach (string n in cur)
            {
                if (!Int32.TryParse(n, out vs[idx]))
                {
                    vs[idx] = -999; // mark for skip later
                }
                idx++;
            }
            idx = 0;
            foreach (string n in cmp)
            {
                if (!Int32.TryParse(n, out cs[idx]))
                {
                    cs[idx] = -999; // mark for skip later
                }
                idx++;
            }
            for (int i = 0; i < len; i++)
            {
                // skip non digits
                if ((vs[i] == -999) || (cs[i] == -999))
                {
                    continue;
                }
                if (vs[i] < cs[i])                 
                {                     
                    return (false);                 
                }                 
                else if (vs[i] > cs[i])
                {
                    return (true);
                }
            }
            return (true);
        }
        public static bool IsVersionLater(this string v1, string v2)
        {
            // split into groups
            string[] cur = v1.Split('.');
            string[] cmp = v2.Split('.');
            // get max length and fill the shorter one with zeros
            int len = Math.Max(cur.Length, cmp.Length);
            int[] vs = new int[len];
            int[] cs = new int[len];
            Array.Clear(vs, 0, len);
            Array.Clear(cs, 0, len);
            int idx = 0;
            // skip non digits
            foreach (string n in cur)
            {
                if (!Int32.TryParse(n, out vs[idx]))
                {
                    vs[idx] = -999; // mark for skip later
                }
                idx++;
            }
            idx = 0;
            foreach (string n in cmp)
            {
                if (!Int32.TryParse(n, out cs[idx]))
                {
                    cs[idx] = -999; // mark for skip later
                }
                idx++;
            }
            for (int i = 0; i < len; i++)
            {
                // skip non digits
                if ((vs[i] == -999) || (cs[i] == -999))
                {
                    continue;
                }
                if (vs[i] < cs[i])                 
                {                     
                    return (false);                 
                }                 
                else if (vs[i] > cs[i])
                {
                    return (true);
                }
            }
            return (true);
        }

We skip non digits in comparison, which means, we accept versions such as ‘1.0.x.3‘. The third item will not be considered in comparison.

We can then have the opposite comparison:

1
2
3
4
        public static bool IsVersionEarlier(this string v1, string v2)
        {
            return (!IsVersionLater(v1, v2));
        }
        public static bool IsVersionEarlier(this string v1, string v2)
        {
            return (!IsVersionLater(v1, v2));
        }

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
376 words
Last Post: Using AT keyboard on modern PC using AT-PS2-USB connector
Next Post: How to Extract Multiple Columns from NumPy 2D Matrix?

The Permanent URL is: How to Compare Version Number String in C#?

Leave a Reply