I need to find the fastest way to get a small list of strings or chars in custom order.
I found a lot of questions about sorting a list, but there is no question on the board or web about sorting a small list with a native type, all I found was way more complex.
For me it does not matter if my input list is a string list or char list. My list has 5 - 7 items, they look like this: "1","Q","A","8","9". I want to get my input ordered like this: "2", "3", "4", "5", "6", "7", "8", "9", "1", "J", "Q", "K", "A".
I tried the following codes:
1 = 3.1-3.5 milliseconds :
static readonly List<String> codeValueSortOrder = new List<String>
{
"2", "3", "4", "5", "6", "7", "8", "9", "1", "J", "Q", "K", "A"
};
input.OrderBy(i => codeValueSortOrder.IndexOf(i.ToString())).ToList();
2 = 5.0-6.0 milliseconds:
input.OrderBy(i => i[1] == 'A')
.ThenBy(i => i[1] == 'K')
.ThenBy(i => i[1] == 'Q')
.ThenBy(i => i[1] == 'J')
.ThenBy(i => i.Substring(1, i.Length - 1) == "10")
.ThenBy(i => i[1] == '9')
.ThenBy(i => i[1] == '8')
.ThenBy(i => i[1] == '7')
.ThenBy(i => i[1] == '6')
.ThenBy(i => i[1] == '5')
.ThenBy(i => i[1] == '4')
.ThenBy(i => i[1] == '3')
.ThenBy(i => i[1] == '2')
.ToList();
I also looked at a few projects at codeproject, but this codes are for million of items, I can′t imagine that they are the most efficient way if we just want to sort between 5 and 7 items. My goal is to perform the sorting in under 0.1 milliseonds, I have no idea if it is possible to accomplish that goal :)
See Question&Answers more detail:
os