Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CBA_fnc_sortNestedArray - Preserve order of elements with same sorting value #1271

Merged
merged 2 commits into from
Jan 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep the apply to save N pushBack commands.

private _helperArray = _array apply {
    [_x select _index, _coefficient * _forEachIndex, _x]
};

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no _forEachIndex in apply

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dang.


_helperArray sort _order;

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

_array