How to sort a keyed collection?

J

Jon Slaughter

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

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1294424&SiteID=1

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

Thanks,
Jon
 
M

Marc Gravell

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

KeyedCollection<TKey,TValue> is dictionary-based, and is built around
IEqualityComparer<TKey>, not IComparer<TKey> - via GetKeyForItem.

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

Jon Slaughter

Marc Gravell said:
Why not just use a SortedList<TKey,TValue>?

KeyedCollection<TKey,TValue> is dictionary-based, and is built around
IEqualityComparer<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(says 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top