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

Conversation

mharis001
Copy link
Contributor

@mharis001 mharis001 commented Dec 27, 2019

When merged this pull request will:

  • title

Example (before changing to default behaviour):

_a = [
    ["a", 100],
    ["b", 200],
    ["e", 100],
    ["d", 150],
    ["c", 100],
    ["abc", 200]
];

[_a, 1, true, true] call CBA_fnc_sortNestedArray;
// [["a",100],["e",100],["c",100],["d",150],["b",200],["abc",200]]

_a pushBack ["x", 100];
// [["a",100],["e",100],["c",100],["d",150],["b",200],["abc",200],["x",100]]

[_a, 1, true, true] call CBA_fnc_sortNestedArray;
// [["a",100],["e",100],["c",100],["x",100],["d",150],["b",200],["abc",200]]

// above, ["x", 100] is sorted after all the elements that appeared before it
// however, if preserve is false, the ordering of elements relative to each other is not preserved
[_a, 1, true, false] call CBA_fnc_sortNestedArray;
// [["e",100],["c",100],["x",100],["a",100],["d",150],["abc",200],["b",200]]

@commy2
Copy link
Contributor

commy2 commented Dec 31, 2019

I don't get it.

@mharis001
Copy link
Contributor Author

Updated the example to be more clear. My use case for this is an array of actions (each with a priority). This addition will allow actions to be sorted in terms of decreasing priority (like current), but also keep actions with the same priority in the order they were added - instead of performing additional unwanted sorting on the full "action sub-arrays".

@commy2
Copy link
Contributor

commy2 commented Dec 31, 2019

keep actions with the same priority in the order they were added

Shouldn't that always be the case?

instead of performing additional unwanted sorting on the full "action sub-arrays"

Seems like a bug/oversight to me.

I think this does not need additional syntax, but just always sort the way your alt syntax is now.

This is the original: https://github.com/CBATeam/CBA_A3/blob/79d52a8338fc9457e6830530794ab6ed03eefd4b/addons/arrays/fnc_sortNestedArray.sqf

@mharis001 mharis001 changed the title CBA_fnc_sortNestedArray - Add option to preserve order of elements with same sorting value CBA_fnc_sortNestedArray - Preserve order of elements with same sorting value Dec 31, 2019

{
_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.

@commy2 commy2 added this to the 3.14 milestone Dec 31, 2019
@commy2 commy2 added the Bug Fix label Dec 31, 2019
@commy2 commy2 self-assigned this Dec 31, 2019
@commy2 commy2 merged commit 08ff228 into CBATeam:master Jan 11, 2020
@mharis001 mharis001 deleted the sortNestedArray-preserve branch January 12, 2020 00:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants