.NET中是否有排序的集合类型?

我正在寻找一个能够保持所有物品整齐的容器。 我查看了SortedList,但这需要一个单独的密钥,并且不允许重复密钥。 我也可以使用未分类的容器,并在每次插入后显式排序。

用法:

  • 偶尔插入
  • 经常遍历顺序
  • 理想情况下,不使用与实际对象分开的键,使用比较函数进行排序。
  • 期望对等效对象进行稳定的排序,但不是必需的。
  • 不需要随机访问。

我意识到我可以建立一个平衡的树结构,我只是想知道框架是否已经包含这样的野兽。

您可能想看一下Wintellect Power Collections 。 它可以在CodePlex上使用,并包含很多非常有用的集合。 项目中的OrderedBag集合正是您正在寻找的。 它基本上使用红黑树来提供非常有效的排序。

只是为了让EBarr的评论作为答案,自.NET 4.0以来就有SortedSet 。 当然它是一个集合,这意味着你不能有重复。

我会扩展你自己的列表类,如你所提到的,只需在每次插入后进行排序。 由于您的插入很少,因此性能损失很小,无论如何,快速排序几乎排序的列表很快。 扩展通用列表并覆盖Add方法以立即排序。 如果性能成为问题,您可以插入到位以节省一些时间。 此外,您可以对插入进行排队,以便为要插入的所有值执行单次遍历插入。

如果您只想坚持使用标准集合,那么List<>类的Sort(IComparer<>)函数通常会被忽略。 您需要做的就是为对象创建一个合适的Comparer<> 。 例如:

 public class PositionDateComparer : IComparer { public int Compare(VehiclePosition x, VehiclePosition y) { if (x.DateTime == DateTime.MinValue) { if (y.DateTime == DateTime.MinValue) { // If x is null and y is null, they're // equal. return 0; } // If x is null and y is not null, y // is greater. return -1; } // If x is not null... // if (y.DateTime == DateTime.MinValue) // ...and y is null, x is greater. { return 1; } // ...and y is not null, compare the dates // if (x.DateTime == y.DateTime) { // x and y are equal return 0; } if (x.DateTime > y.DateTime) { // x is greater return 1; } // y is greater return -1; } } 

然后只要在访问列表之前对列表进行排序,就执行vehiclePositionsList.Sort(new PositionDateComparer()) 。 我意识到这可能不像每次添加新对象时自动排序的容器那么简单,但对于许多人(比如我!)而言,这可能足以成功完成工作而无需任何其他库。

正如我今天早些时候在这里提到的, C5通用集合库有适合你的容器。

如果键也是对象的属性,则可以尝试System.Collections.ObjectModel.KeyedCollection 。 它是一个抽象类,但如果你的键只是项目的属性,那么它的衍生起来非常简单。

这是我在VB6中用来按字母顺序排序的旧技巧:使用System.Windows.Forms ListBox对象,并将其“Sorted”属性设置为true。 在C#中,您可以将任何对象插入到列表框中,它将按字母顺序对其ToString()值进行排序:

对于一个类模块:


使用System.Windows.Forms;

  static void Main(string[] args) { ListBox sortedList = new ListBox(); sortedList.Sorted = true; sortedList.Items.Add("foo"); sortedList.Items.Add("bar"); sortedList.Items.Add(true); sortedList.Items.Add(432); foreach (object o in sortedList.Items) { Console.WriteLine(o); } Console.ReadKey(); } 

这将显示:

432
酒吧
FOO
真正