How to sort a hashtable?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: How to sort a hashtable?

  1. #1
    Join Date
    Jan 2012
    Posts
    18

    Question How to sort a hashtable?

    I have a hashtable with different words(key) and the number of times they occur(value).
    I want to sort it based on value in such a way that the word which occurs for the most no. of times appears first.How do I do so?

  2. #2
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    10,955

    Re: How to sort a hashtable?

    Can you update the outdated hashtable into a generic collection like:
    System.Collections.Generic.SortedDictionary<> or
    System.Collections.Generic.Dictionary<> ?

  3. #3
    Join Date
    May 2007
    Posts
    1,546

    Re: How to sort a hashtable?

    This is impossible, it is not how a hashtable works. What you want to do is something like:

    Assuming you have a Dictionary<K, V> you can do something like:

    Dictionary<string, int> foo = GenerateDictionary ();
    List<string> resultsInOrder = foo.OrderBy (k => k.Value).Select (k => k.Key).ToList ();

    This will take all the KeyValuePairs in the dictionary, order them based on their value and finally select just the key component (the string part) and put them in a list.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center