Does T-SQL have any ability to do custom comparison of strings (for sorting), equivalent to .NET's IComparer
?
Something like the ability to give Order By
a user defined function that takes 2 strings and returns a value representing how they compare (greater than, less that, equal to)?
I currently have a C# ICompararer
implementation that is being used to sort things in code, but now I need to produce this same sorted output from a stored proc.
For reference, this is the IComparer
that I am trying to implement in TSQL.
public class SemanticComparer : IComparer<string>
{
private static Regex _splitter = new Regex("\\W+");
public int Compare(string x, string y)
{
string[] partsX = _splitter.Split(x);
string[] partsY = _splitter.Split(y);
int shortest = Math.Min(partsX.Length, partsY.Length);
for (int index = 0; index < shortest; index++)
{
int intX, intY;
int result;
if (int.TryParse(partsX[index], out intX) && int.TryParse(partsY[index], out intY))
{
result = intX.CompareTo(intY);
}
else
{
result = string.Compare(partsX[index], partsY[index], StringComparison.Ordinal);
}
if (result != 0)
{
return result;
}
}
return 0;
}
}
It would need to be able to sort things that look like this (in order as they should be output):
- 2-101.11(A)(B)
- 9.1
- 9.2
- 9.2.1
- 9.02.2
- 9.02.3
- 9.3
- 9.3.1
- 9.3.2
- 10.
- 11.
- 11.1
- 11.2.a
- 11.2.b
- 11.2.c
- 11a.2.a
- 11b.2.b
- 21 CFR 110.10
- 046.981(e)(m)
Where each non-word character splits the string in to segments, tries to compare them numerically if possible, and then as strings if not. There can be any "depth" to the number of segments.
CLR Stored Procedures are, unfortunately, not an option.