With this function you can sort an array in ascending order or descending order or using a custom function to define the sort order. The function requires you to provide a variable that holds the array, and then either:

If you use a function to create the sort order, then it must take 2 arguments (these will correspond to two elements within the array), and the function should return 0 for equality, less than 0 or greater than 0. The return value will then be used to determine the sort order (see the example given below).

Note that if the array contains a set of strings, then the strings will be sorted alphabetically based on the english alphabet when using the default ascending/descending sort type. All other data types will be sorted based on numerical value, the exact values of which will depend on the data type itself (for example, an array of buffers would be sorted based on the converted numerical value of their memory address).



array_sort(variable, sorttype_or_function);

Argument Description
variable The variable that holds the array.
sorttype_or_function  The sort type (true for ascending or false for descending) or a function reference to use for sorting.






var _xx, _yy, _a;
for (var i = 0; i < 10; ++i;)
    _xx = irandom(room_width);
    _yy = irandom(room_height)
    _a[i] = instance_create_layer(_xx, _yy, layer, obj_Bullet);


var _f = function(inst1, inst2)
    return inst1.x - inst2.x;


The above code will create an array of 10 instances placed at random positions within the room. The debug message will show something like the following:

[ 100003,100004,100005,100006,100007,100008,100009,100010,100011,100012 ]

The code then creates a method to be used in the array_sort() function that returns the difference between each of the X positions of the instances in the room and the array_sort() function is run using the method, sorting the instances by X from lowest to highest. The output for the array would then look like this:

[ 100011,100004,100009,100006,100012,100008,100010,100005,100007,100003 ]