Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jk15 · Jun 18, 2012 at 03:06 PM · c#listdictionarysorting

Sorting a List of Dictionaries in C#?

Help!

I am new to C# and have spent all day trying to work out a way to do the following.

Read a XML file that contains a player's name and player's score and store it in a variable. I have been using a Dictionary.

Place all these dictionaries into a List.

Sort the list of dictionaries by the high score. (can't work this out)

Have the ability to add another new score and sort again.

Write the results back to the file I loaded it from.

Is the approach I'm taking wrong using a List of Dictionaries? Any advice would be a great help.

Thanks.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wolfram · Jun 18, 2012 at 03:31 PM 0
Share

This is not really a Unity question, Stack Overflow would be a better forum for this.

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by jk15 · Jun 19, 2012 at 02:50 AM

Thanks for all the suggestions - in the end I simplified it. Dictionaries were overkill and I created a value object to store the players name and score. I then used OrderBy that was mentioned in an answer above to order on one of the vo properties.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by whydoidoit · Jun 18, 2012 at 03:37 PM

  using System.Linq;

  var sortedList = myListOfDictionaries.OrderBy(d=>d["yourScoreName"]).ToList();
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wolfram · Jun 18, 2012 at 03:37 PM 0
Share

I knew you'd post somethign like this :D

avatar image whydoidoit · Jun 18, 2012 at 03:48 PM 1
Share

I would also suggest you consider making classes that are X$$anonymous$$LSerialized. $$anonymous$$uch better than a dictionary:

 public class $$anonymous$$yList
 {
      [XmlElement("NameOfThePlayerInfo")]
      public PlayerInfo[] Info;
 }

 public class PlayerInfo
 {
      [XmlAttribute]
      public string Name;
      [XmlAttribute]
      public int Score;
 }

The use XmlSerializer to deserialize $$anonymous$$yList and it will contain a list of the players.

avatar image
0

Answer by Berenger · Jun 18, 2012 at 03:16 PM

Starting C# by sorting a list of dictionary seems a bit bold, but anyway :

You need to use the Sort function. You need a function to compare two dictionary (I will assume it's a dictionary) :

 private static int CompareDictionariesByScore (Dictionary<string, int> x,
                                             Dictionary<string, int> y)
 {
     if (x == null)
     {
         if (y == null)
         {
             // If x is null and y is null, they're
             // equal. 
             return 0;
         }
         else
         {
             // If x is null and y is not null, y
             // is greater. 
             return -1;
         }
     }
     else
     {
         // If x is not null...
         //
         if (y == null)
             // ...and y is null, x is greater.
         {
             return 1;
         }
         else
         {
             // ...and y is not null, compare the 
             // lengths of the two dictionaries.
             int xScore = x[ "Score" ];
             int yScore = y[ "Score" ];
 
             if( xScore == yScore ) return 0;
             else if( xScore > yScore ) return 1;
             else return -1;
         }
     }
 }


Then, you can sort the list that way

 list.Sort( CompareDictionariesByScore );

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Wolfram · Jun 18, 2012 at 03:30 PM

Why not use a SortedDictionary instead?

Also, there's about a gazillion useful google hits for that:

http://stackoverflow.com/questions/289/how-do-you-sort-a-c-sharp-dictionary-by-value

http://stackoverflow.com/questions/451717/how-do-i-convert-from-a-dictionary-to-a-sorteddictionary-using-linq-in-c

http://stackoverflow.com/questions/5949904/c-sharp-sort-dictionary-with-linq

http://www.dotnetperls.com/sort-dictionary

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# List Reported as Empty when called in method 1 Answer

From two lists into a dictionary... and back? 5 Answers

Sort list game objects based on name 3 Answers

Find index of item in list 1 Answer

Runtime instantiation based on XML 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges