How do you sort KeyedCollection <TKey, TItem>?

Mostly I have one KeyedCollection<string, CustomNode>, and I want to be able to sort the collection using a key (or preferably using a custom mapping).

If this is not possible, can someone recommend another class in which the key is embedded in a value that I can sort?

+3
source share
6 answers

This question came up trying to solve a similar problem. If this is still relevant, I was able to implement Sort using the example here:

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

Mainly includes sorting the base list of the collection. Worked like a charm for me.

Hooray!

+3
source

KeyCollection<T> Collection<T>, IEnumerable, IEnumerable.OrderBy(). IEnumerable.OrderBy() , .

+2

(. answer ), "", .

BindableLinq ( ) OrderBy .

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

PropertyChanged, . , , INotifyCollectionChanged.

+2

, : 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;
      }
   }

I have not tested it yet, but it works fine.

+1
source

Why not just use a class SortedList<TKey, TValue>?

MSDN Link

-1
source

You can see the SortedDictionary collection ... But this will incur additional costs for finding O (log N) positions as opposed to KeyedCollection with O (1) extraction.

-1
source

Source: https://habr.com/ru/post/1784866/


All Articles