none
Need c# code to find the biggest one among the version numbers. RRS feed

  • Question

  • hi,

    I need c# code to find the biggest version no among the given version numbers.

    ex versions nos: 3.0.19, 2.11.1, 1.22.123

    Biggest Version no: 3.0.19

    Thanks

    Thursday, December 20, 2018 10:36 AM

Answers

  •     class Version
        {
            public int Major { get; set; }
            public int Minor { get; set; }
            public int Build { get; set; }
            public override string ToString()
            {
                return string.Format("{0}.{1}.{2}", Major, Minor, Build);
            }
    
        }
        class VersionComparer : IComparer<Version>
        {
            public int Compare(Version x, Version y)
            {
                int diff = y.Major - x.Major;
                if (diff != 0)
                    return diff;
                diff = y.Minor - x.Minor;
                if (diff != 0)
                    return diff;
                return y.Build - x.Build;
            }
        }
    
        static void Main(string[] args)
        {
            List<Version> versions = new List<Version>
            {
                new Version{ Major=1, Minor=22, Build=123},
                new Version{ Major=2, Minor=11, Build=1},
                new Version{Major=3, Minor=0, Build=19}
            };
            versions.Sort(new VersionComparer());
            Console.WriteLine(versions[0].ToString());    
        }


    • Marked as answer by Arivazhagan K Thursday, December 20, 2018 1:08 PM
    Thursday, December 20, 2018 11:39 AM