简体   繁体   English

你如何排序KeyedCollection <TKey,TItem>?

[英]How do you sort a KeyedCollection<TKey, TItem>?

Basically I've got a KeyedCollection<string, CustomNode> , and I want to be able to sort the collection by the key (or preferably with a custom comparer). 基本上我有一个KeyedCollection<string, CustomNode> ,我希望能够通过键(或者最好使用自定义比较器)对集合进行排序。

If this isn't possible, can someone recommend another class where the key is embedded in the value that I can sort? 如果这是不可能的,有人可以推荐另一个类,其中键嵌入在我可以排序的值中吗?

Came this question while trying to solve a similar problem. 在尝试解决类似问题时遇到了这个问题。 If it is still relevant, I was able to implement Sort using the example here: 如果仍然相关,我可以使用此处的示例实现Sort:

http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/56adc0f9-aa1b-4acf-8546-082bb01058f2/ http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/56adc0f9-aa1b-4acf-8546-082bb01058f2/

Basically involves sorting the underlying List of the collection. 基本上涉及对集合的基础List进行排序。 Worked like a charm for me. 对我来说就像一个魅力。

Cheers! 干杯!

KeyCollection<T> inherits from Collection<T> which implements IEnumerable so you should be able to use IEnumerable.OrderBy() . KeyCollection<T>继承自Collection<T> ,它实现了IEnumerable因此您应该能够使用IEnumerable.OrderBy() IEnumerable.OrderBy() also has an overload that allows you to supply a custom comparer . IEnumerable.OrderBy()也有一个重载, 允许您提供自定义比较器

Upon further information (see comments on answer above), a requirement is to keep the "set" sorted by a element's property after the property is edited. 根据进一步的信息(参见上面的答案评论),要求是在编辑属性后保持“set”按元素的属性排序。

In this case, you might take a look at BindableLinq (there are other similar frameworks too) and use the OrderBy statement implemented in there. 在这种情况下,您可以查看BindableLinq (还有其他类似的框架)并使用其中实现的OrderBy语句。

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

As long as your edited property raises a PropertyChanged event then it'll apply the re-ordering immediately. 只要您编辑的属性引发PropertyChanged事件,它就会立即应用重新排序。 If you wish to change your collection, then ensure that the source collection implements INotifyCollectionChanged. 如果要更改集合,请确保源集合实现INotifyCollectionChanged。

This is based on the link provided in the answer by Dan: http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/56adc0f9-aa1b-4acf-8546-082bb01058f2/ 这是基于Dan的答案中提供的链接: http//social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/56adc0f9-aa1b-4acf-8546-082bb01058f2/

   public class RequestTemplate : IComparable<RequestTemplate>
   {
      // This is the primary key for the object
      private Guid _guidNumber;

      // This is what a collection of these objects should be sorted by
      private string _buttonCaption = "";


      public Guid GuidNumber
      {
         get { return _guidNumber; }
         set { _guidNumber = value; }  // Setter only provided for deserialization usage
      }

      public string ButtonCaption
      {
         get { return _buttonCaption; }
         set { _buttonCaption = value; }
      }


      /// <summary>
      /// Method needed to allow sorting a collection of these objects.
      /// </summary>
      public int CompareTo(RequestTemplate other)
      {
         return string.Compare(this.ButtonCaption, other.ButtonCaption, 
                               StringComparison.CurrentCultureIgnoreCase);
      }
   }


   public class RequestTemplateKeyedCollection : KeyedCollection<Guid, RequestTemplate>
   {
      /// <summary>
      /// Sort the collection by sorting the underlying collection, accessed by casting the Items 
      /// property from IList to List.
      /// </summary>
      public void Sort()
      {
         List<RequestTemplate> castList = base.Items as List<RequestTemplate>;
         if (castList != null)
            castList.Sort();  // Uses default Sort() for collection items (RequestTemplate)
      }


      /// <summary>
      /// Method needed by KeyedCollection.
      /// </summary>
      protected override Guid GetKeyForItem(RequestTemplate requestTemplate)
      {
         return requestTemplate.GuidNumber;
      }
   }

Haven't tested it extensively yet, but it seems to work OK. 还没有广泛测试它,但它似乎工作正常。

Why not just use a SortedList<TKey, TValue> Class? 为什么不使用SortedList<TKey, TValue> Class?

MSDN link MSDN链接

您可以查看SortedDictionary集合......但是,与带有O(1)检索的KeyedCollection相比,这将带来项目检索O(log N)的额外费用。

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

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