How to retrieve top 10 elements in a Dictionary

This is a discussion on How to retrieve top 10 elements in a Dictionary within the C# Programming forums, part of the General Programming Boards category; Hello everyone, Suppose I have a Dictionary object instance, Dictionary<string, int>, the int is always positive. I want to get ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    How to retrieve top 10 elements in a Dictionary

    Hello everyone,


    Suppose I have a Dictionary object instance, Dictionary<string, int>, the int is always positive. I want to get top 10 elements from the Dictionary with the maximum 10 int values, any efficient way to do this?


    thanks in advance,
    George

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Store them in a SortedDictionary and iterate over the last 10 elements (perhaps by using a custom comparer, making the highest number appear first)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scheduling Algo
    By BigDaddyDrew in forum C++ Programming
    Replies: 41
    Last Post: 03-08-2003, 10:00 AM
  2. What are the top 10 things a windows programmer should know
    By anobody in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-12-2002, 10:47 AM
  3. Heaps...
    By Nutshell in forum C Programming
    Replies: 14
    Last Post: 04-23-2002, 08:54 AM
  4. Formatting Output
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 03-26-2002, 12:33 AM
  5. Favorite top 10 poster
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 11-04-2001, 09:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21