以自定义顺序获取列表的最快方法 [英] Fastest way to get list in custom order

查看:43
本文介绍了以自定义顺序获取列表的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到最快的方法来按自定义顺序获取一小串字符串或字符. 我发现了很多有关对列表进行排序的问题,但是在董事会或网络上都没有关于对具有本机类型的小列表进行排序的问题,我发现所有这些都变得更加复杂.

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.

对我来说,输入列表是字符串列表还是字符列表都没有关系.我的列表中有5-7个项目,它们看起来像这样:"1","Q","A","8","9".我想按以下顺序排列我的输入:"2","3","4","5","6","7","8","9","1","J", "Q","K","A".

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".

我尝试了以下代码:

1 = 3.1-3.5毫秒:

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毫秒:

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();

我还查看了codeproject上的几个项目,但是这些代码用于数百万个项目,如果我们只想在5到7个项目之间排序,我无法想象它们是最有效的方法.我的目标是在0.1毫秒内执行排序,我不知道是否有可能实现该目标:)

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 :)

推荐答案

您可以使用Dictionary<string, int>,其中键是字符串,值是索引.

You could use a Dictionary<string, int> where the key is the string and the value is the index.

您甚至还可以使用List<string>作为基础:

You can even still use the List<string> as basis:

private static readonly List<string> _List = new List<string> { "2", "3", "4", "5", "6", "7", "8", "9", "1", "J", "Q", "K", "A" };
static readonly Dictionary<string, int> Order = _List
    .ToDictionary(str => str, str => _List.IndexOf(str) + 1);

现在,您可以使用 ,随着LINQ的临近,它不需要创建新列表:

Now you're able to use List.Sort which does not need to create a new list as the LINQ approaches:

var input = new List<string> { "1", "Q", "A", "8", "9" };
int i1, i2;
input.Sort((s1, s2) =>
{
    Order.TryGetValue(s1, out i1);
    Order.TryGetValue(s2, out i2);
    return i1.CompareTo(i2);
});

结果顺序:8,9,1,Q,A

一个快速测试显示:StopWatch.Elapsed.TotalMilliseconds 0.0045

A quick test revealed: StopWatch.Elapsed.TotalMilliseconds 0.0045

Dictionary.TryGetValue如果找到字符串,则返回索引,否则返回0.这就是为什么我在上面使用_List.IndexOf(str) + 1强制找到的项目排在最后.

Dictionary.TryGetValue returns either the index if the string was found or 0 otherwise. That's why i've used _List.IndexOf(str) + 1 above to force that found items come last.

如果要降序,只需将其反转:

If you want a descending order you just have to reverse it:

return i2.CompareTo(i1);

这篇关于以自定义顺序获取列表的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆