Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

How to sort a keyed collection?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jon Slaughter - 17 Mar 2008 21:49 GMT
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
Marc Gravell - 18 Mar 2008 00:11 GMT
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?
Jon Slaughter - 18 Mar 2008 00:25 GMT
> 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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.