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
2
Question by Paparakas · Nov 14, 2013 at 02:39 PM · valuedictionarysortingsort

How do you sort a dictionary by value?

There are a lot of resources on google, but none of them seem to work. It doesn't let me use a SortedDictionary either.

I'm trying to get an index of a list of GameObjects, and assign their GetDistanceFromPlayer return value as the value of the dictionary. Then I want to sort it and use the values to make a new list of GameObjects with the distance sorted from lowest to highest. I'm obviously not asking you to write my code for me, but it would be great to get an actual functional function I can use to at least start in the right direction.

Comment
Add comment · Show 8
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 Jamora · Nov 14, 2013 at 02:44 PM 0
Share

Seems to me you won't need a dictionary at all if you sorted the list directly using the GetDistanceFromPlayer-method.

avatar image Paparakas · Nov 14, 2013 at 02:45 PM 0
Share

@Jamora So it's possible to sort a list of GameObjects by one of their members?

avatar image Jamora · Nov 14, 2013 at 02:50 PM 0
Share

Yes, as long as the members can be meaningfully compared to each other.

You cant really compare two GameObjects, but you could compare e.g. their transform's x-position. When doing that, you just need to remember which member is part of which GameObject.

avatar image GameVortex · Nov 14, 2013 at 03:03 PM 0
Share

yes, you can sort a single list of GameObjects with the .Sort function of List:

 List<GameObject> objList = new List<GameObject>();
 
 //Fill with gameobjects...
 
 
 objList.Sort((x,y) => GetDistanceFromPlayer(x).ComareTo(GetDistanceFromPlayer(y));


$$anonymous$$eep in $$anonymous$$d that this is not the most efficient way because it will call the function GetDistanceFromPlayer multiple times for each gameObject.

avatar image Paparakas · Nov 14, 2013 at 03:22 PM 0
Share

@GameVortex How would I go about comparing the distance variable in your example? Isn't your example assu$$anonymous$$g that the GetDistanceFromPlayer variable is contained within the class sorting the list?

Show more comments

2 Replies

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

Answer by GameVortex · Nov 14, 2013 at 06:13 PM

This is a general way of sorting a list of CustomClass based on a custom variable in the class:

 List<CustomClass> list = new List<CustomClass>();
 
 //Fill with objects of CustomClass...
 
 list.Sort((x,y) => x.CustomIntVariable.ComareTo(y.CustomIntVariable));
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 TonyLi · Nov 14, 2013 at 02:55 PM

If you really need to use a dictionary (and not a SortedList as @Jamora suggests), try this: http://stackoverflow.com/a/291

Comment
Add comment · Show 4 · 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 Paparakas · Nov 14, 2013 at 03:07 PM 0
Share
 foreach ($$anonymous$$eyValuePair<int,float> item in ActiveCrabs.OrderBy(key=> ActiveCrabs[key].GetComponent<CrabScript>().DistanceFromPlayer))
             { 
                 // do something with item.$$anonymous$$ey and item.Value
             }

It's not working. What am I doing wrong?

avatar image TooManySugar Paparakas · Jan 27, 2016 at 11:28 AM 0
Share

You solved? where you adding this in the header of your script?

System.Linq

avatar image Munchy2007 TooManySugar · Jan 27, 2016 at 11:39 AM 0
Share

After more than two years I imagine he's probably forgotten all about it :p

avatar image Dracorat · Nov 14, 2013 at 06:18 PM 0
Share

Just want to point out that sorting is usaully overkill on a Dictionary. The whole point is to use the key to look things up. If you want a SortedList, you could just use it directly rather than incurring the overhead of a Dictionary.

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

22 People are following this question.

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

Related Questions

sorting list of key, value by value 1 Answer

Order an array of classes using 2 variables (Racing game, ranking system) 1 Answer

get key of item from dictionary based on value c# 1 Answer

Comparing Dictionary values 1 Answer

Dictionary wont give me value with correct key 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