Scroll to top

C# Language : IComparable


Curious Bot - December 1, 2018 - 0 comments

Sort versions

Class:

public class Version : IComparable<Version>
{
public int[] Parts { get; }

public Version(string value)
{
if (value == null)
throw new ArgumentNullException();
if (!Regex.IsMatch(value, @"^[0-9]+(\.[0-9]+)*$"))
throw new ArgumentException("Invalid format");
var parts = value.Split('.');
Parts = new int[parts.Length];
for (var i = 0; i < parts.Length; i++)
Parts[i] = int.Parse(parts[i]);
}

public override string ToString()
{
return string.Join(".", Parts);
}

public int CompareTo(Version that)
{
if (that == null) return 1;
var thisLength = this.Parts.Length;
var thatLength = that.Parts.Length;
var maxLength = Math.Max(thisLength, thatLength);
for (var i = 0; i < maxLength; i++)
{
var thisPart = i < thisLength ? this.Parts[i] : 0;
var thatPart = i < thatLength ? that.Parts[i] : 0;
if (thisPart < thatPart) return -1;
if (thisPart > thatPart) return 1;
}
return 0;
}
}

Test:

Version a, b;

a = new Version("4.2.1");
b = new Version("4.2.6");
a.CompareTo(b); // a < b : -1

a = new Version("2.8.4");
b = new Version("2.8.0");
a.CompareTo(b); // a > b : 1

a = new Version("5.2");
b = null;
a.CompareTo(b); // a > b : 1

a = new Version("3");
b = new Version("3.6");
a.CompareTo(b); // a < b : -1

var versions = new List<Version>
{
new Version("2.0"),
new Version("1.1.5"),
new Version("3.0.10"),
new Version("1"),
null,
new Version("1.0.1")
};

versions.Sort();

foreach (var version in versions)
Console.WriteLine(version?.ToString() ?? "NULL");

Output:

NULL
1
1.0.1
1.1.5
2.0
3.0.10

Demo:

Live demo on Ideone

Related posts