I have a ListCollectionView which holds a collection of scene objects and I need to sort it by scene Number (Scene.SceneNumber) which is a string containing numbers and letters and I need to sort it in a very specific order. First by number then by letter with the lowercase letters first, then the uppercase.
The list needs to look like this:
1
1pt1
1pt2
1A
1Apt1
2
2pt1
2Apt1
3
...
This is what I have so far:
public class SceneComparer : IComparer
{
private readonly Regex sceneRegEx = new Regex(@"(\d+)(\w+)?", RegexOptions.Compiled);
public int Compare(object x, object y)
{
Scene sceneX = x as Scene;
Scene sceneY = y as Scene;
var firstSceneMatches = this.sceneRegEx.Matches(sceneX.SceneNumber);
var secondSceneMatches = this.sceneRegEx.Matches(sceneY.SceneNumber);
var matchesCount = Math.Min(firstSceneMatches.Count, secondSceneMatches.Count);
for (var i = 0; i < matchesCount; i++)
{
var firstSceneMatch = firstSceneMatches[i];
var secondSceneMatch = secondSceneMatches[i];
var firstSceneNumeric = Convert.ToInt32(firstSceneMatch.Groups[1].Value);
var secondSceneNumeric = Convert.ToInt32(secondSceneMatch.Groups[1].Value);
if (firstSceneNumeric != secondSceneNumeric)
{
return firstSceneNumeric - secondSceneNumeric;
}
var firstSceneAlpha = firstSceneMatch.Groups[2].Value;
var secondSceneAlpha = secondSceneMatch.Groups[2].Value;
if (firstSceneAlpha != secondSceneAlpha)
{
return string.CompareOrdinal(firstSceneAlpha, secondSceneAlpha);
}
}
return firstSceneMatches.Count - secondSceneMatches.Count;
}
}
this results in following list
1
1A
1Apt1
1pt1
1pt2
2
2Apt1
2pt1
...
But I need the lowercase letters before the uppercase letters. How do I do this?