[英]C# Structure that sorts key pairs and is accessible?
Noob here. Noob在这里。 I've been scouring the internet for days, and cannot find a decent structure that auto-sorts data (like SortedSet), while still allowing that data to be accessible (like List). 我已经在网上搜了好几天了,找不到一个像自动排序数据的体面结构(比如SortedSet),同时仍然可以访问这些数据(比如List)。 Here's what I have: 这就是我所拥有的:
A list containing 100,000 nodes, added and modified regularly. 包含100,000个节点的列表,定期添加和修改。
List<Nodes> nodes;
The node object, containing data I need to access/change 节点对象,包含我需要访问/更改的数据
public class Node (string name, int index){ doSomething(); }
I don't wish to be vague, but can't sort the actual list because the index is a history of when nodes were added. 我不希望模糊,但不能对实际列表进行排序,因为索引是添加节点的历史记录。 Thus, I want to use a structure that auto-sorts KeyValuePair pairs(where string is the name to be sorted by, and int is the index as it is found in my list of nodes), but I must be able to access the value. 因此,我想使用一个自动排序KeyValuePair对的结构(其中string是要排序的名称,int是在我的节点列表中找到的索引),但我必须能够访问该值。 Here's what I want to do: 这就是我想要做的事情:
// Add a node to the list, then to the structure
int index = nodes.Count;
nodes.Add(new Node("someName", index));
someStructure.Add("someName", index);
// Give name to structure, which returns int value for use in finding node
node[someStructure.findValueOf("someName"))].doSomething();
This would tell the node with the name "someName" to doSomething(); 这将告诉节点名为“someName”的doSomething();
I am positive that I am missing something. 我很肯定我错过了什么。 I've tried using SortedSet, SortedList, Dictionary, etc. In each case, I can't retrieve the sorted object. 我尝试过使用SortedSet,SortedList,Dictionary等。在每种情况下,我都无法检索已排序的对象。 What is the purpose of auto-sorting if I can't find out where they are at? 如果我找不到它们的位置,自动排序的目的是什么? Please help my poor life. 请帮助我的穷人生活。
You are looking for a SortedDictionary
. 您正在寻找SortedDictionary
。
As per the documentation: Represents a collection of key/value pairs that are sorted on the key.
根据文档: Represents a collection of key/value pairs that are sorted on the key.
Although, as some comments say, those 100k objects would be better kept in a database... 虽然,正如一些评论所说,那些100k对象最好保存在数据库中......
Link: https://msdn.microsoft.com/en-us/library/f7fta44c(v=vs.110).aspx 链接: https : //msdn.microsoft.com/en-us/library/f7fta44c(v = vs.110).aspx
You can use SortedList
and LINQ: 您可以使用SortedList
和LINQ:
SortedList<int,string> list = new SortedList<int, string>();
list.Add(1, "name1");
list.Add(2, "name2");;
var c = list.Select(x => x.Value == "name2").FirstOrDefault();
However I agree with a Christopher's comment about using db. 但是我同意克里斯托弗关于使用db 的评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.