CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2001
    Posts
    2,455

    Sorting a dictionary.

    I need to sort a dictionary by value. I have the following code:
    Code:
            public void Sort()
            {
                List<KeyValuePair<string, PluginDomain>> list = new List<KeyValuePair<string, PluginDomain>>(_domains);
                list.Sort(delegate(KeyValuePair<string, PluginDomain> a, KeyValuePair<string, PluginDomain> b)
                {
                    return a.Value.Ordinal.CompareTo(b.Value.Ordinal);
                });
    The problem with the above, is that it sorts the list just fine, how to I copy the contents of the list back to the dictionary?

    Do I have to iterate through the list and add to the dictionary one object at a time or is there a better way?

    Mike B

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

    Re: Sorting a dictionary.

    You can't sort a dictionary by value. It makes no sense. Dictionaries are for finding a value *really fast* based on a key. What are you trying to accomplish?
    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.

  3. #3
    Join Date
    Feb 2001
    Posts
    2,455

    Re: Sorting a dictionary.

    Quote Originally Posted by Mutant_Fruit View Post
    You can't sort a dictionary by value. It makes no sense. Dictionaries are for finding a value *really fast* based on a key. What are you trying to accomplish?
    I don't see why this doesn't make sences, this isn't a sorted dictionary and there is no defined order of keys in a regular dictionary object.

    I want to be able to refer to an object in the dictionary by name (which is the key) but I also want to order the items by value for listing the objects in the GUI.

    Mike

  4. #4
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Sorting a dictionary.

    Quote Originally Posted by MikeB View Post
    I don't see why this doesn't make sences, this isn't a sorted dictionary and there is no defined order of keys in a regular dictionary object.
    there is a defined order of keys in a dictionary. they are ordered by their hash code (ie key.GetHashCode)

    what you're attempting to use is a sort of map. since there's no map data structure, why not create your own? its the sort of basic stuff they make you do in school anyway so it shouldn't be that difficult.

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

    Re: Sorting a dictionary.

    Quote Originally Posted by MikeB View Post
    I don't see why this doesn't make sences, this isn't a sorted dictionary and there is no defined order of keys in a regular dictionary object.
    There is no defined order *of the values*. It is entirely implementation specific. If you want to be able to order the values alphabetically (or something like that), then you need a different data structure. A dictionary is the wrong thing completely to be using.
    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.

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Sorting a dictionary.

    In general when you want to be able to access data by two difference means, the best solution is two different datastructures.

    This is common enough that I have developed a set of general classes that contain one (or more) dictionaries for searching (multiple keys) and one or more lists for ordered accessing.

    Given that (typically) the items are reference tyype items, there is little memory overhead.

    The "big" question is when to sort the list:

    a) Agressively (on insert, delete or change)
    b) Lazy (on retrieval)

    [my classes contain a flag which controls this behaviour....]
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  7. #7
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Lightbulb Re: Sorting a dictionary.

    it is not possible to sort a dictionary (Dictionary<keyType, valueType>) but you can use a trick,
    you can copy the contents of the dictionary to a generic list (List<string>) and then implement a Comparison delegate and sorting method for the list then sorting it.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured