Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ----------------------------------------------------------------------------
- Function: fnc_sortNestedArray2
- Based: CBA_fnc_sortNestedArray
- Author: Dimon UA
- Description:
- Used to sort a nested array from lowest to highest or from highest to lowest using quick sort based on the specified column, which must have numerical data.
- Parameters:
- _array: array - Nested array to be sorted
- _index: integer - sub array item index to be sorted on
- _sort: true - from highest to lowest, false - from lowest to highest
- Example:
- (begin example)
- _array = [_array,1,_sort] call fnc_sortNestedArray2
- (end)
- Returns:
- Passed in array
- Author:
- Standard algorithm
- ---------------------------------------------------------------------------- */
- /*
- Modified BIS function to sort nested arrays.
- Added 2nd parameter to indicate which column (index in nested array) to sort on.
- Sorts an array of numbers from lowest (left) to highest (right).
- The passed array is modified by reference.
- This function uses the quick sort algorithm.
- */
- private ["_re_sort","_debug"];
- _re_sort = {
- private ["_h","_i","_j","_hi","_x","_sort","_id","_lo","_a"];
- _a = _this select 0; //array to be sorted
- _id = _this select 1;
- _lo = _this select 2;//lower index to sort from
- _hi = _this select 3;//up
- _sort = _this select 4;//true - from highest to lowest, false - from lowest to highest
- _h = nil; //used to make a do-while loop below
- _i = _lo;
- _j = _hi;
- if (count _a == 0) exitWith {};
- _x = (_a select ((_lo + _hi) / 2)) select _id;
- // partition
- while {isnil "_h" || {_i <= _j}} do
- {
- //find first and last elements within bound that are greater / lower than _x
- if _sort then
- {
- while {(_a select _i) select _id > _x} do {_i = (_i) + 1};
- while {(_a select _j) select _id < _x} do {_j = (_j) - 1};
- }else{
- while {(_a select _i) select _id < _x} do {_i = (_i) + 1};
- while {(_a select _j) select _id > _x} do {_j = (_j) - 1};
- };
- if (_i <= _j) then {
- //swap elements _i and _j
- _h = _a select _i;
- _a set [_i, _a select _j];
- _a set [_j, _h];
- _i = (_i) + 1;
- _j = (_j) - 1;
- };
- };
- // recursion
- if (_lo < _j) then {[_a, _id, _lo, _j,_sort] call _re_sort};
- if (_i < _hi) then {[_a, _id, _i, _hi,_sort] call _re_sort};
- };
- // and start it off
- _debug = false;
- [_this select 0, _this select 1, 0, 0 max ((count (_this select 0))-1), _this select 2] call _re_sort;
- if _debug then
- {
- {
- call compile format ["
- _m%1 = createMarker[""markerBrown%1"",[ _x select 0,_x select 1]];
- _m%1 setMarkerShape ""ICON"";
- _m%1 setMarkerType ""DOT"";
- _m%1 setmarkercolor ""Colorbrown""; ",_forEachIndex];
- } foreach (_this select 0);
- };
- // array is already modified by reference, but return the modified array anyway
- _this select 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement