简体   繁体   English

C#批量添加到排序列表

[英]C# Bulk Add to a Sorted List

I am looking for the fastest and most efficient way to use a sorted list or array under 2 different conditions. 我正在寻找在2种不同条件下使用排序列表或数组的最快,最有效的方法。 I wanna have my cake and eat it too! 我想吃我的蛋糕,也吃!
First, I need to bulk add several hundred key/value pairs (possibly as many as 20,000) to the list/array and then have them sorted by key. 首先,我需要向列表/数组中批量添加几百个键/值对(可能多达20,000个),然后按键对它们进行排序。 QuickSort is probably the best way to do it, figuring out the insertion point and shuffling items after every addition certainly is not. QuickSort可能是最好的方法,确定插入点并在每次添加后改组项目肯定不是。
Afterwards, I need to occasionally add key/value pairs to the list/array on an as-needed basis and have them inserted at the right spot. 之后,我需要根据需要偶尔将键/值对添加到列表/数组,并将它们插入正确的位置。 Re-running QuickSort after every insertion is probably not the best way to do it. 每次插入后重新运行QuickSort可能不是最好的方法。
And of course the reason I want it sorted is to do quick B-Tree lookups to find the value based on the key. 当然,我希望对它进行排序的原因是进行快速的B树查找以基于键查找值。
[FYI: In one case, The key is a Guid and the value is a struct or simple class with a string (human-friendly name) and a pair of ints. [FYI:在一种情况下,键是Guid,值是带有字符串(人类友好名称)和一对整数的结构或简单类。 In the other case, the string is the key. 在另一种情况下,字符串是关键。 Unfortunately, the string/name is not guaranteed to be unique. 不幸的是,不能保证字符串/名称唯一。 Everything is held in RAM because speed and responsiveness to the user is crucial. 一切都保存在RAM中,因为对用户的速度和响应能力至关重要。 All the initial data that is bulk added is generated by another app and I have no control over it.] 批量添加的所有初始数据都是由另一个应用生成的,我无法控制它。]
I have some ideas on how to write a helper class to do this, but I'd rather not reinvent the wheel. 我对如何编写一个帮助程序类来做到这一点有一些想法,但是我不想重蹈覆辙。 Yet I cannot figure out a way to do it with the built in list or collection classes. 但是我无法找到一种使用内置列表或集合类的方法。 The SortedList class does not appear to have a bulk add, and the regular list or array classes (once sorted) don't have a way to insert by key. SortedList类似乎没有批量添加,常规列表或数组类(一旦排序)没有按键插入的方法。

Suggestions? 有什么建议吗?

You should consider the use of a Dictionary instead of a List , so long as you will be searching for elements based only on the key and not the value. 只要您仅基于键而不是值搜索元素,就应该考虑使用Dictionary而不是List This is an O(1) operation when trying to get a value for a single key, as opposed to having to make many comparisons to find the same object in a list. 尝试获取单个键的值时,这是O(1)操作,而不是必须进行多次比较才能在列表中找到同一对象。

var dictionary = new Dictionary<string, string>
{
    {"e11a5c0e-58b4-4f3c-86b8-6a127fff6ee8", "A"},
    {"7c1fdecf-7f75-4538-8805-50a67652a5a3", "B"},
    {"fed8892c-bb18-4fe3-8e31-8e287afa9243", "C"}
};

if (dictionary.TryGetValue("fed8892c-bb18-4fe3-8e31-8e287afa9243", out var value))
{
    Console.WriteLine($"The value is: {value}");
}

Also, if you could avoid using a GUID and use a sequential int or long , you would be able to add to the end of the list, right? 另外,如果您可以避免使用GUID并使用连续的intlong ,则可以将其添加到列表的末尾,对吗? No matter how you go about it, when you use a GUID as your key you're always going to have to make a number of comparisons to insert and swap the other elements to keep the list sorted. 无论如何处理,当您使用GUID作为键时,您总是必须进行多次比较才能插入并交换其他元素,以使列表保持排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM