For a project I'm working on, I need to compare version strings where:version strings are composed only of numbers and periodsversion strings are made up of an arbitrary number of segments (major.minor, manjor.minor.build, etc)1.0 is equivalent to 1.0.0, etc.Please provide feedback on performance considerations or style.Here's my shot at it:public class VersionStringComparer : IComparer<string>{    public int Compare(string x, string y)    {        if (x == y) return 0;        var xparts = x.Split('.');        var yparts = y.Split('.');        var length = new[] {xparts.Length, yparts.Length}.Max();        for (var i = 0; i < length; i++)        {            int xint;            int yint;            if (!Int32.TryParse(xparts.ElementAtOrDefault(i), out xint)) xint = 0;            if (!Int32.TryParse(yparts.ElementAtOrDefault(i), out yint)) yint = 0;            if (xint > yint) return 1;            if (yint > xint) return -1;        }        //they're equal value but not equal strings, eg 1 and 1.0        return 0;    }}And two quick unit tests (using the Should.Fluent library:    [TestMethod]    public void VersionStringComparerSortsMajorAndMinorVersionString()    {        var versions = new[]{1.5,1.0,2.0,1.0};        var versionStringComparer = new ReleaseGateway.Util.VersionStringComparer();        var sorted = versions.OrderBy(x => x, versionStringComparer).ToArray();        var expected = new[] {1.0, 1.0, 1.5, 2.0};        sorted.Should().Equal(expected);    }    [TestMethod]    public void VersionStringComparerSortsArbitraryRadixVersionStrings()    {        var versions = new[] { 1.5.4, 1.7, 1.0, 1,1.4 };        var versionStringComparer = new ReleaseGateway.Util.VersionStringComparer();        var sorted = versions.OrderBy(x => x, versionStringComparer).ToArray();        var expected = new[] { 1.0, 1, 1.4, 1.5.4, 1.7 };        sorted.Should().Equal(expected);    }
VersionString (eg 1.0.2) IComparer algorithm
c#;algorithm;linq;unit testing