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?
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.
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.
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.
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 ;
}
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.
Think I'd take the Linq methods over that, to be perfectly honest.
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.
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.