Max of two package versions

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string version1 = "3.3-2";
            string version2 = "5.3-2";
            Console.WriteLine("Version1 = " + version1);
            Console.WriteLine("Version2 = " + version2);
            //Compare the versions
            string[] greaterSubVersion = GetGreaterVersion(GetSubVersion(version1), GetSubVersion(version2));
            //Print the greater version
            Console.WriteLine("Greater Version = " + greaterSubVersion[0] + "." + greaterSubVersion[1] + "-" + greaterSubVersion[2]);
        }
        public static string[] GetGreaterVersion(string[] version1, string[] version2)
        {
            int flag = 0;
            for (int i = 0; i < version1.Length; i++)
            {
                if (int.Parse(version1[i]) > int.Parse(version2[i]))
                {
                    flag = 1;
                    break;
                }
                else if (int.Parse(version2[i]) > int.Parse(version1[i]))
                {
                    flag = 2;
                    break;
                }
            }
            if (flag < 2)
            {
                return version1;
            }
            return version2;
        }
        public static string[] GetSubVersion(string version)
        {
            string[] subVersion = new string[3];
            Regex reg = new Regex("\\.");
            subVersion[0] = reg.Split(version).GetValue(0).ToString();
            string tempStr = reg.Split(version).GetValue(1).ToString();
            reg = new Regex("-");
            subVersion[1] = reg.Split(tempStr).GetValue(0).ToString();
            subVersion[2] = reg.Split(tempStr).GetValue(1).ToString();
            return subVersion;
        }
    }
}

Output
=======
Version1 = 3.3-2
Version2 = 5.3-2
Greater Version = 5.3-2
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s