To be able to sort data on non-numeric field and get sorted list in numeric order, you have to do some trick. You need a
dictionary[
^], where
TKey
should be
Detail and
TValue
should be corresponding numeric value. That's all!
Dictionary<string, int> DetailsDict = new Dictionary<string, int>();
DetailsDict.Add("x3", 3);
DetailsDict.Add("x4", 4);
DetailsDict.Add("x10extra", 15);
DetailsDict.Add("x11", 16);
Usage:
var result = ...
.OrderBy(a=>DetailsDict[a.Detail]);
Second solution
Another way is to write extension method as follow:
static int GetMyNumber(string test)
{
int retVal = 0;
Int32.TryParse(test.Replace("c","").Replace("extra","").Replace("x",""), out retVal);
return retVal;
}
Usage:
var result = ...
.Select(a=>new
{
origVal = a.Detail,
newVal = GetMyNumber(a.Detail)
}
)
.OrderBy(a=>a.newVal).ThenBy(a=>a.origVal);