How to sort scriptableobject in a list

 public List<Units> unitsID = new List<Units>();

 unitsID.OrderBy(s => s.MaxSpeed).ToList(); 
 unitsID.ForEach(p => Debug.Log(p.MaxSpeed));

It doesn’t sort as I expected

1 Answer

1

You need to assign the OrderBy results back into the unitsID reference.

unitsID = unitsID.OrderBy(s => s.MaxSpeed).ToList();

A more efficient solution is:

unitsID.Sort ((x, y) => x.MaxSpeed.CompareTo (y.MaxSpeed));