how sort works?

So I looked at the documentation and its of no help.

I looked at a few of the possible related questions and didnt really see a good explanation.

Basically what I want is

I’m using C#

I have an ArrayList (should I change to List? i’m unclear on whats considered best, its kind of odd actually how ambigious this seems Array, List, ArrayList, its odd for such an established and successful game engine. :P)

the array list is an array of vector3

i would like to sort by the Y Component only (actually at some point each component but I can infer the code for X and Z from Y)

some kind of

array[3].y > array[2].y > array[1].y

or the reverse.

I promise I don’t normally ask for a code handout its just that the documentation doesnt seem to be there for this. I’m thinking it should be just one line

c# doesn’t konw how compare elements try to impement IComparer interface. this will help IComparer Interface (System.Collections) | Microsoft Learn

The fastest option is builtin arrays, like this:

Vector3[] points = new Vector3[100];

You can sort them with Array methods like Array.Sort, where you can define the comparison function.

But these arrays have fixed sizes - if you want to dynamically add or remove elements, List is the best option (don’t use ArrayList, it’s way slower). You can use List methods to sort the array as well, like List.Sort.

The Unity docs only cover the Unity engine - google for “whatever you want” + “.Net” to find info about the .Net/Mono specific functions (the Unity docs should give us this hint!)

I actually JUST wrote that exact functionality, so good luck is yours:

	private var orderComparer : IComparer = new DrawOrderComparer();
	function get sortedAUVs() : ArrayList { sortableAUVs.Sort(orderComparer); return sortableAUVs; }
	private var sortableAUVs : ArrayList;

class DrawOrderComparer implements IComparer {
	static var t1: AnimatedUV;
	static var t2: AnimatedUV;
	function Compare( a : Object, b : Object ) : int {
		auv1 = a as Transform;
		auv2 = b as Transform;
		if ( auv1.position.y > auv2.position.y ) return 1;
		if ( auv1.position.y < auv2.position.y ) return -1;
		return 0;

As for Array/ArrayList/List:

Never use Array.

List is great; a little slower than builtin but lots of helper functions for resizing it or adding/removing specific elements, and significantly more manageable than Array.

array (“builtin” array) is always fastest, but its size isn’t dynamic - if you need to change how many elements it contains, you need to rebuild it from scratch.

I never use ArrayList, so I don’t know much about it. Apparently it has a nice overrideable Sort function. x) It looks like List has a sort function just like ArrayList - I’d recommend using that (Dangit now I’ve gotta go back in and change up my sorter :))