Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by arisonu123 · Jul 20, 2017 at 04:31 AM · vector3listsortinglinq

Sort a list of class by a vector variable in the class

So I have a list of a class, say MyObjectClass. The MyObjectClass has a Vector3 variable. I want to sort my list of MyObjectClass objects by the x in this Vector3 Variable. I want it sorted by lowest to highest x and if two variables have the same x, I want them sorted by lowest to highest y.

So if MyObjectClass list contains MyClassObject's that's vector variables is say the following Vectors: (135,62,0) , (120,47,0) (150,30,0), and (120,28,0)

I want the list to be sorted so that calls to the get function for each objects vector variable results in this order: (120,28,0) , (120,47,0), (135,62,0) , and (150, 30, 0).

Is there a way, perhaps with linq or some other method, to sort them with this criteria, or will I have to write my own sorting algorithm to sort them?

Comment
Add comment
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

2 Replies

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

Answer by Vicarian · Jul 20, 2017 at 04:56 AM

You definitely don't have to reinvent the wheel to implement your own sort. Linq has this functionality. To go about it:

 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;

 public class ExampleClass : MonoBehaviour {
     private List<MyObjectClass> m_myObjectClasses;

     void Start() {
         m_myObjectClasses = new List<MyObjectClass>();

         m_myObjectClasses.Add(new MyObjectClass(new Vector3(135,62,0)));
         m_myObjectClasses.Add(new MyObjectClass(new Vector3(120,47,0)));
         m_myObjectClasses.Add(new MyObjectClass(new Vector3(150,30,0)));
         m_myObjectClasses.Add(new MyObjectClass(new Vector3(120,28,0)));

         foreach (MyObjectClass m in SortedMyObjectClasses)
             Debug.Log(m.m_vector);

         // Output:
         // (120, 28, 0)
         // (120, 47, 0)
         // (135, 62, 0)
         // (150, 30, 0)
     }
     public IOrderedEnumerable<MyObjectClass> SortedMyObjectClasses
     {
         get
         {
             return m_myObjectClasses.OrderBy(m => m.m_vector.x).ThenBy(m => m.m_vector.y);
         }
     }
 }

 public class MyObjectClass
 {
     public Vector3 m_vector;

     public MyObjectClass(Vector3 vector)
     {
         m_vector = vector;
     }
 }

Obviously your class would differ, and you might not use a constructor to initialize the vector in MyObjectClass, but the accessor on SortedMyObjectClasses is what you need.

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 arisonu123 · Jul 20, 2017 at 05:08 PM 0
Share

Thanks. It appears to be working great. I'm not very familiar with Linq so I wasn't sure exactly how to use it to sort my list of objects but was pretty sure it could.

avatar image Vicarian arisonu123 · Jul 20, 2017 at 05:18 PM 1
Share

No problem. Linq's syntax certainly takes some getting used to, but if you've had exposure to SQL or some other query language, that makes it easier, since you can map those functions to the strange Linq syntax. I started working on desktop apps before Unity, so having that background has definitely helped.

If you don't want to maintain an unsorted list of the objects, you can just as easily assign the result of that Linq statement back to the original collection by:

 m_myObjectClasses = m_myObjectClasses.OrderBy(m => m.m_vector.x).ThenBy(m => m.m_vector.y).ToList();

The return type of the OrderBy extension methods is IOrderedEnumerable, but invoking ToList() on the result gets an object that's cast to List.

avatar image
1

Answer by Hellium · Jul 20, 2017 at 07:49 AM

Simpler method:

 void Start() {
      myObjectClasses = new List<MyObjectClass>();
      myObjectClasses.Add(new MyObjectClass(new Vector3(135,62,0)));
      // ....
      myObjectClasses.Sort( Compare );
  }

  private int Compare( MyObjectClass a, MyObjectClass b )
  {
         Vector3 va = a.Vector ;
         Vector3 vb = b.Vector ;

        if( Mathf.Abs( va.x - vb.x ) < 0.001f )
        {
              if( Mathf.Abs( va.y - vb.y ) < 0.001f  ) return 0 ;
              return va.y < vb.y ? -1 : 1 ;               
        }
        return va.x < vb.x ? -1 : 1 ;        
  }

Comment
Add comment · Show 12 · 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 Hellium · Jul 20, 2017 at 07:49 AM 0
Share

https://stackoverflow.com/questions/24187287/c-sharp-listt-orderby-float-member

avatar image Vicarian · Jul 20, 2017 at 12:43 PM -1
Share

Comment invalidated due to update of original source.

avatar image arisonu123 · Jul 20, 2017 at 05:19 PM 0
Share

Vicarian is right this only sorts by X and then doesn't check for duplicate X's with different Y's. I'm guessing after I sort them by X I could then sort by Y but I also think then it might only be sorted by Y and get rid of the X sorting. I've never used the List sort function before so I'm not sure.

avatar image Hellium arisonu123 · Jul 20, 2017 at 07:39 PM 0
Share

Answer updated ;)

avatar image Vicarian Hellium · Jul 20, 2017 at 07:41 PM 0
Share

Think I'd take the Linq methods over that, to be perfectly honest.

Show more comments
avatar image Owen-Reynolds · Jul 20, 2017 at 09:03 PM -1
Share

This is also the standard way we're been doing sorts for the last 20+ years: pass it a 2 input -1/0/1 less/equal/more comparer function. Programmers who have never seen Unity or C# would understand this method.

Linq is made for people with more database experience than program$$anonymous$$g. Things like "OrderBy," and "ThenBy" are meant to look more like SQL strings.

avatar image Vicarian Owen-Reynolds · Jul 20, 2017 at 09:08 PM 0
Share

This comment is disingenuous at best. I have nearly 20 years experience writing code, from C++ to VB, to Java, to other now dead languages. But just to save typing and bracing, I do like OrderBy(Func).ThenBy(Func). I've written insertion sorts, bubble sorts, etc, but while doing so I always have to make sure I don't flub my references due to an errant keystroke.

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

82 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

Related Questions

Some LINQ functions won't work. And no; not iOS 0 Answers

Sort list by distance AND by being active 3 Answers

Total war Style Movement System with Right Click 0 Answers

How do I properly use List-Vector3-.Contains()? 1 Answer

GameObject move to each Vector3s in a list 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