I've just stumbled upon a SQF implementation of a bubblesort. Since bubblesort is known to perform terribly bad, here is a quicksort (wrapped in a function library template):
/* libStd.sqf
*
* NOTES: This file must be initialized from init.sqf like this:
*
* call compile preprocessFileLineNumbers "libStd.sqf";
*
* Feel free to adjust the #defines to make them meet your
* naming scheme.
*/
#define FN_QUICKSORT fnQuicksort
FN_QUICKSORT = {
/* SYNTAX: [Array, FirstIndex, LastIndex] call FN_QUICKSORT;
*
* PARAMETERS: Array: Array to be sorted
* FirstIndex: Indicates where to start sorting
* LastIndex: Indicates where to stop sorting
*
* EXAMPLE: [_myArray, 0, (count _myArray) - 1] call fnQuicksort;
*/
private ["_a", "_l", "_r", "_i", "_j", "_t"];
_a = _this select 0;
_l = _this select 1;
_r = _this select 2;
if (_r > _l) then {
_i = _l - 1;
_j = _r;
_t = 0;
while { true } do {
scopeName "whileTrue";
while { _i = _i + 1; (_a select _i) < (_a select _r) } do {};
while { _j = _j - 1; ((_a select _j) > (_a select _r)) && (_j > _i)} do {};
if (_i >= _j) then { breakOut "whileTrue" };
_t = _a select _i;
_a set [_i, _a select _j];
_a set [_j, _t];
};
_t = _a select _i;
_a set [_i, _a select _r];
_a set [_r, _t];
[_a, _l, _i - 1] call FN_QUICKSORT;
[_a, _i + 1, _r] call FN_QUICKSORT;
};
}; // FN_QUICKSORT