1

Currently I get the Release Version from a website using Selenium and it is stored as a string.

The Release Versions are 14.14,14.14.4,14.15,14.15.1,14.15.2 etc.

I need to sort them using C# and the Database Admin needs to be able to sort them using SQL. All of the Release Version items are currently stored as strings.

I do all my SQL stuff using LINQ to SQL if that matters.

What is the best way to sort them in both C# and SQL? As strings? Or should I convert them? What data type would be used if converting them?

Jonathan Kittell
  • 7,163
  • 15
  • 50
  • 93
  • 1
    To avoid having to write sorting code in both C# and SQL, you could start saving the versions with fixed width numbering `014.015.001` so lexical sort works naturally in both, but obviously you may not want or have control over that. – AaronLS Sep 25 '14 at 22:16
  • In C#, you could just provide a custom IComparer. http://msdn.microsoft.com/en-us/library/bb534863.aspx – user2880486 Sep 25 '14 at 22:25

2 Answers2

2

In C# you could store them as (or at least convert to) Version which also supports correct sorting:

var strings = new List<string> { "14.14", "14.14.4", "14.15", "14.15.1", "14.15.2" };
var versions = strings.Select(v => Version.Parse(v)).ToList();
versions.Sort();  // or OrderBy in the LINQ query above

Here's a question which shows how to sort versions in the database: How Can I Sort A 'Version Number' Column Generically Using a SQL Server Query

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

If you are fine with converting it back and forth, then convert the version to an int. since there are max 3 parts for the version, just convert it to {0} * 10000 + {1} * 100 + {2}

Steve
  • 11,696
  • 7
  • 43
  • 81