c# - 如何对 KeyedCollection<TKey, TItem> 进行排序?

标签 c# generics sorting collections keyedcollection

基本上我有一个 KeyedCollection<string, CustomNode> ,并且我希望能够按键(或者最好使用自定义比较器)对集合进行排序。

如果这不可能,有人可以推荐另一个类,其中键嵌入在我可以排序的值中吗?

最佳答案

根据更多信息(参见上面关于 answer 的评论),要求在编辑属性后保持“集合”按元素的属性排序。

在这种情况下,您可以查看 BindableLinq (也有其他类似的框架)并使用其中实现的 OrderBy 语句。

KeyedCollection<string, CustomNode> collection = /* from whereever */
collection.Items.AsBindable().OrderBy(c => c.PropertyOnCustomNode);

只要您编辑的属性引发 PropertyChanged 事件,它就会立即应用重新排序。如果您希望更改您的集合,请确保源集合实现 INotifyCollectionChanged。

关于c# - 如何对 KeyedCollection<TKey, TItem> 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4664228/

相关文章:

c++ - C++11 中的通用 STL 兼容直方图

arrays - $lookup 结果使用另一个数组中的键排序

java - 比较器 : How to sort on two parameter when one is instance variable and other is reference in the Same Class

c# - 为什么要使用 yield 关键字,而我只能使用普通的 IEnumerable?

c# - 在列表 C# 中创建列表

c# - Web 部署项目是否使用 x86 构建配置

c# - 有没有更有效的方法在 C# 中创建我的 DataTable?

generics - ceylon max{} 的返回类型

c# - "Store"稍后在 C# 中使用的类型参数?

java - 如何按字母顺序对数组排序,标点符号优先