简体   繁体   English

SortedList只需要IComparable <T>

[英]SortedList that just takes IComparable<T>

I have an interface IScriptItem that implements IComparable<IQueueItem> . 我有一个接口IScriptItem ,它实现了IComparable<IQueueItem> In my eyes it would seem enough to have IComparable items in order to have a sorted anything. 在我看来,似乎有足够的IComparable项目来排序任何东西。 But all I can find is Dictionaries, Hashtables and SortedLists that are actually SortedTrees. 但我能找到的只是Dictionaries,Hashtables和SortedLists,它们实际上是SortedTrees。

What I'm looking for is a sorted generic list that takes IComparables. 我正在寻找的是一个排序的通用列表,它采用IComparables。 Am I looking in the wrong places? 我在错误的地方看?

There's nothing built-in. 内置任何东西都没有。 You have some options: 你有一些选择:

  • Use SortedList with a dummy TValue. 将SortedList与虚拟TValue一起使用。
  • Use a list or array and call List.Sort() or Array.Sort() when necessary. 使用列表或数组,并在必要时调用List.Sort()或Array.Sort()。
  • Write your own. 写你自己的。
  • Use a third party library 使用第三方库

For this particular case check out Wintellect PowerCollections OrderedBag class, which uses a red-black tree internally. 对于这个特殊情况,请查看Wintellect PowerCollections OrderedBag类,它在内部使用红黑树。 Other good free data structure libraries include NGenerics and C5. 其他好的免费数据结构库包括NGenerics和C5。

If I understand correctly, you want an SortedCollection< TValue> instead of the various SortedCollection< TKey, TValue> that are there. 如果我理解正确,你需要一个SortedCollection <TValue>而不是那里的各种SortedCollection <TKey,TValue>。

In .NET 4 there is a new SortedSet<T> class that keeps its items in order, but doesn't allow duplicates. 在.NET 4中,有一个新的SortedSet<T>类,它保持其项目的顺序,但不允许重复。 Otherwise you'll have to look at 3rd party options, like the one Matt Howells mentions. 否则你将不得不看第三方选项,如Matt Howells提到的选项。

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

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