How to sort a keyed collection?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Slaughter

    #1

    How to sort a keyed collection?

    topic says it all? Is there any simple way? msdn says that the keyed
    collection is build from both a sortedlist and sorted dictionary.

    http://msdn2.microsoft.com/en-us/library/5z658b67.aspx

    (well, it doesn't say that specifically but it says it when talking about
    the sorted versions so I'm not sure)

    I've found this



    That will probably work but hoping that there is some internal framework to
    do it(or maybe they need to create a SortedKeyedColl ection too?)

    Thanks,
    Jon


  • Marc Gravell

    #2
    Re: How to sort a keyed collection?

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

    KeyedCollection <TKey,TValuei s dictionary-based, and is built around
    IEqualityCompar er<TKey>, not IComparer<TKey- via GetKeyForItem.

    If you want to sort it, perhaps just copy the values out?

    Comment

    • Jon Slaughter

      #3
      Re: How to sort a keyed collection?


      "Marc Gravell" <marc.gravell@g mail.comwrote in message
      news:5e5ca17e-d9a0-4a8d-83b7-503fbecfb540@h1 1g2000prf.googl egroups.com...
      Why not just use a SortedList<TKey ,TValue>?
      >
      KeyedCollection <TKey,TValuei s dictionary-based, and is built around
      IEqualityCompar er<TKey>, not IComparer<TKey- via GetKeyForItem.
      >
      If you want to sort it, perhaps just copy the values out?
      Its a bit faster than the sorted lists and dictionaries(sa ys O(1) and almost
      O(1))

      I used a comparer to do it and it seems to work fine. Was hoping there was
      something a bit more easier but I guess not.

      I need to sort the list based on a string key but have fast access.
      SortedList is O(logn) for indexing while keyedcollection is O(1). Since I
      don't need to have a "continuous " sort I think just using the comparer will
      work fine.


      Comment

      Working...