Sorting GameObjects In An Array Alphabetically

Can somebody explain to me how the compare in .Sort() works? For example ordering gameObjects by name?

You need to supply a function to provide the comparison - turns out the Unity Script syntax is a bit strange and you have to pass in the array to its own sort function. It looks like this:

       objs.Sort(objs, function(g1,g2) String.Compare(g1.name, g2.name));

The function provided must take 2 game objects and:

  • Return 0 if they are the same
  • Return -1 if g2 is greater than g1
  • Return 1 if g2 is less that g1

String.Compare will automatically do that for strings.