BIS fnc sortBy: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (GVI http://dev.arma3.com/post/spotrep-00004 First mentioned here, could be 0.50 (no evidence))
m (Some wiki formatting)
 
(26 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{RV|type=function


{{Function|= Comments
|game1= arma3
____________________________________________________________________________________________
|version1= 0.58


| arma3 |= Game name
|gr1= Arrays


|0.58|= Game version
|descr= Sorts an array according to given algorithm. See [[sort]] for simple sorts.
____________________________________________________________________________________________


| <pre>/*
|s1= [array, parameters, algorithm, direction, filter] call [[BIS_fnc_sortBy]]


Description:
|p1= array: [[Array]] - any unsorted array. Can contain any types
Sorts an array according to given algorithm.


Parameter(s):
|p2= parameters: [[Array]]
_this select 0: any unsorted array (Array)
* used in the eval algorithm
- array can contain any types (object/numbers/strings ..)
* input params are referenced in the sorting algorithm by _input0..9
* a maximum of 10 input params is supported (0-9)


_this select 1: input parameters (Array)
|p3= algorithm: [[Code]] - (Optional, default <sqf inline>{ _x }</sqf>)
- used in the eval algorithm (object/numbers/strings ..)
* needs to return [[String]], [[Number]] or [[Array]] - see [[sort]]
- input params are referenced in the sorting algorithm by _input0, _input1 .. _input9
* [[Magic Variables#x|_x]] refers to array item
- max. number of 10 input params is supported (0-9)


_this select 2: sorted algorithm (Code) [optional: default {_x}]
|p4= direction: [[String]] - (Optional, default "ASCEND") case-insensitive
- code needs to return a scalar
* "ASCEND" for ascending sorting
- variable _x refers to array item
* "DESCEND" (or anything else) for descending sorting


_this select 3: sort direction (String) [optional: default "ASCEND"]
|p5= filter: [[Code]] - (Optional, default <sqf inline>{ true }</sqf>) code that needs to evaluate true for the array item to be sorted, otherwise item is removed
"ASCEND": sorts array in ascending direction (from lowest value to highest)
"DESCEND": sorts array in descending direction


_this select 4: filter (Code) [optional: default {true}]
|r1= [[Array]] - sorted array
- code that needs to evaluate true for the array item to be sorted, otherwise item is removed


Returns:
|x1= <sqf>
Array
// sort numbers from lowest to highest
_sortedNumbers = [[1,-80,0,480,15,-40], [], {_x}, "ASCEND"] call BIS_fnc_sortBy;
</sqf>


Examples:
|x2= <sqf>
// sort helicopters by distance from player
_closestHelicopters = [[_heli1,_heli2,_heli3], [], { player distance _x }, "ASCEND"] call BIS_fnc_sortBy;
</sqf>


//sort numbers from lowest to highest
|x3= <sqf>
_sortedNumbers = [[1,-80,0,480,15,-40],[],{_x},"ASCEND"] call BIS_fnc_sortBy;
// sort enemy by distance from friendly unit (referenced by local variable), the furthest first
 
_furtherstEnemy = [[_enemy1,_enemy2,_enemy3], [_friendly], { _input0 distance _x }, "DESCEND", { canMove _x }] call BIS_fnc_sortBy;
//sort helicopters by distance from player
</sqf>
_closestHelicopters = [[_heli1,_heli2,_heli3],[],{player distance _x},"ASCEND"] call BIS_fnc_sortBy;
 
//sort enemy by distance from friendly unit (referenced by local variable), the furtherest first
_furtherstEnemy = [[_enemy1,_enemy2,_enemy3],[_friendly],{_input0 distance _x},"DESCEND",{canMove _x}] call BIS_fnc_sortBy;
*/
 
</pre><small>''(Placeholder description extracted from the function header by [[BIS_fnc_exportFunctionsToWiki]])''</small> |= Description
____________________________________________________________________________________________
 
| <!-- [] call [[BIS_fnc_sortBy]]; --> |= Syntax
 
|p1= |= Parameter 1
 
| |= Return value
____________________________________________________________________________________________
 
|x1= <code></code> |=
____________________________________________________________________________________________
 
| |= See also


|seealso= [[BIS_fnc_sortAlphabetically]] [[BIS_fnc_sortNum]] [[sort]]
}}
}}
<h3 style="display:none">Notes</h3>
<dl class="command_description">
<!-- Note Section BEGIN -->
<!-- Note Section END -->
</dl>
<h3 style="display:none">Bottom Section</h3>
[[Category:Function Group: Arrays|{{uc:sortBy}}]]
[[Category:Functions|{{uc:sortBy}}]]
[[Category:{{Name|arma3}}: Functions|{{uc:sortBy}}]]

Latest revision as of 14:57, 13 July 2022

Hover & click on the images for description

Description

Description:
Sorts an array according to given algorithm. See sort for simple sorts.
Execution:
call
Groups:
Arrays

Syntax

Syntax:
[array, parameters, algorithm, direction, filter] call BIS_fnc_sortBy
Parameters:
array: Array - any unsorted array. Can contain any types
parameters: Array
  • used in the eval algorithm
  • input params are referenced in the sorting algorithm by _input0..9
  • a maximum of 10 input params is supported (0-9)
algorithm: Code - (Optional, default { _x })
direction: String - (Optional, default "ASCEND") case-insensitive
  • "ASCEND" for ascending sorting
  • "DESCEND" (or anything else) for descending sorting
filter: Code - (Optional, default { true }) code that needs to evaluate true for the array item to be sorted, otherwise item is removed
Return Value:
Array - sorted array

Examples

Example 1:
// sort numbers from lowest to highest _sortedNumbers = [[1,-80,0,480,15,-40], [], {_x}, "ASCEND"] call BIS_fnc_sortBy;
Example 2:
// sort helicopters by distance from player _closestHelicopters = [[_heli1,_heli2,_heli3], [], { player distance _x }, "ASCEND"] call BIS_fnc_sortBy;
Example 3:
// sort enemy by distance from friendly unit (referenced by local variable), the furthest first _furtherstEnemy = [[_enemy1,_enemy2,_enemy3], [_friendly], { _input0 distance _x }, "DESCEND", { canMove _x }] call BIS_fnc_sortBy;

Additional Information

See also:
BIS_fnc_sortAlphabetically BIS_fnc_sortNum sort

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note