DISCLAIMER:
NOt sure if code based solution would work for the user as it is unclear to me where exactly it is being used. But if user has opportunity to write code to sort the versions, following algorithm is strong and will work without any workarounds like prefixing with 0 etc.
ANSWER:
You just need to write a custom comparer and sort it using custom comparer. Below is the logic to convert version of above format into a integer value and use the underlying integer value to sort it.
This is a console app which I just wrote but you get the idea! (Note that I have not put the validation, always assume right input)
class Program
{
public class VersionComparer : IComparer<string>
{
public int Compare(string x, string y)
{
string[] xVersionParts = x.Split('.');
string[] yVersionParts = y.Split('.');
if (xVersionParts[0] != yVersionParts[0])
return int.Parse(xVersionParts[0]).CompareTo(int.Parse(yVersionParts[0]));
else if (xVersionParts[1] != yVersionParts[1])
return int.Parse(xVersionParts[1]).CompareTo(int.Parse(yVersionParts[1]));
else
return int.Parse(xVersionParts[2]).CompareTo(int.Parse(yVersionParts[2]));
}
}
static void Main(string[] args)
{
List<string> versions = new List<string>();
versions.Add("1.0.0");
versions.Add("10.0.0");
versions.Add("10.0.1");
versions.Add("11.0.0");
versions.Add("2.0.0");
versions.Add("2.0.1");
versions.Add("3.0.0");
versions.Add("3.1.0");
versions.Add("3.0.1");
versions.Sort();
Console.WriteLine("Before Custom Comparer...");
foreach (string version in versions)
{
Console.WriteLine(version);
}
versions.Sort(new VersionComparer());
Console.WriteLine("After Custom Comparer...");
foreach (string version in versions)
{
Console.WriteLine(version);
}
Console.ReadLine();
}
}
Here are the results....
