• 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
Question by Feref2 · Nov 01, 2022 at 08:28 PM · listvaluekeysortinglambda

How to sort a list of keyValuePair?

The key is a GameObject and the value is a Vector3. The y and z axis are constant, so only the x changes. Based on t$$anonymous$$s, I want to sort them from left to right. Am understanding that I have to use a lambda expression to ac$$anonymous$$eve t$$anonymous$$s, but I don´t know what that is and am having a hard time figuring it out. Thanks in advance.

Comment

People who like this

0 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 logicandchaos · Nov 01, 2022 at 09:23 PM 0
Share

You don't have to use a lambda expression, you can use a number of sorting algorithms you could employ.

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Bunny83 · Nov 01, 2022 at 09:49 PM

You never have to use a lambda expression. They just make it easier to write inplace anonymous methods.

So based on your descrption you have a

 List<KeyValuePair<GameObject, Vector3>> list;

w$$anonymous$$ch you want to sort based on the x value of the Vector3 value of each pair? You should be able to do t$$anonymous$$s:

 list.Sort((a, b) => a.Value.x.CompareTo(b.Value.x));


As I said, lambda expression are just in place shortcuts for anonymous methods. In addition they can be closures, but that' irrelevant here. You can instead use a method like t$$anonymous$$s

 private static int SortOnValueX(KeyValuePair<GameObject, Vector3> a,KeyValuePair<GameObject, Vector3> b)
 {
     return a.Value.x.CompareTo(b.Value.x);
 }

T$$anonymous$$s is the exact same method and can be used instead:

 list.Sort(SortOnValueX);

That's why lambda expressions are usually simpler.

Comment
logicandchaos

People who like this

1 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 Feref2 · Nov 02, 2022 at 12:23 AM 0
Share

Thanks a lot for your reply. Could you please explain a little bit more this line of code? list.Sort((a, b) => a.Value.x.CompareTo(b.Value.x)); //What are a and b. Am assuming that a is an index for all keyValuePairs that are being compared against the current b...? or something? Sorry if it's a dumb question. It's probably obvious, but I don´t know.

avatar image Bunny83 Feref2 · Nov 02, 2022 at 02:14 AM 0
Share

I just gave you the equivalent normal method. I said:

This is the exact same method

So how do you come to the conclusion a and b would be indices? The Sort method uses the callback to sort the elements in the list. "a" and "b" are simply two elements from the list that should be compared. The method returns an integer value that should indicate if a should become before b or the other way round. This is indicated either by a positive or negative value. When the method returns 0, the two values are the same. The CompareTo method does exactly return such a value.


Again, look at the signature of the static method I posted above. The two arguments a and b have the type KeyValuePair<GameObject, Vector3>. It's simply the element type of your List


If you still have trouble I would recommend you start by reading the documentation of the Sort method. The Sort method has 4 overloads. We use the one that takes a Comparison<T> delegate. See the documentation of that delegate for more information.


This is just standatd C# and nothing Unity specific.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

149 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 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 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 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 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 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 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

A node in a childnode? 1 Answer

Orderrring a List Ascending or Descending C# Advanced 2 Answers

How to get the key from Array value 1 Answer

Sorting a list by three parameters (C#) 1 Answer

Vector3 List Find Contains Specific X Coordinate Value and Remove 0 Answers


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