Skip to content

Commit

Permalink
Merge pull request #1271 from mharis001/sortNestedArray-preserve
Browse files Browse the repository at this point in the history
CBA_fnc_sortNestedArray - Preserve order of elements with same sorting value
  • Loading branch information
commy2 committed Jan 11, 2020
2 parents d2c93f2 + a0a6b5a commit 08ff228
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions addons/arrays/fnc_sortNestedArray.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
Function: CBA_fnc_sortNestedArray
Description:
Used to sort a nested array from lowest to highest using quick sort.
Sorts the given nested array in either ascending or descending order based on the
numerical value at specified index of sub arrays.
Sorting is based on the specified column, which must have numerical data.
Original array is modified.
Parameters:
Expand All @@ -22,20 +22,23 @@ Returns:
Sorted array <ARRAY>
Author:
commy2
commy2, mharis001
---------------------------------------------------------------------------- */
SCRIPT(sortNestedArray);

params [["_array", [], [[]]], ["_index", 0, [0]], ["_order", true, [false]]];

private _helperArray = _array apply {
[_x select _index, _x]
};
private _helperArray = [];
private _coefficient = [-1, 1] select _order;

{
_helperArray pushBack [_x select _index, _coefficient * _forEachIndex, _x];
} forEach _array;

_helperArray sort _order;

{
_array set [_forEachIndex, _x select 1];
_array set [_forEachIndex, _x select 2];
} forEach _helperArray;

_array

0 comments on commit 08ff228

Please sign in to comment.