{"name":"BIS_fnc_sortBy","title":"BIS fnc sortBy","type":"function","url":"https://community.bistudio.com/wiki/BIS_fnc_sortBy","revid":327964,"updated":"2022-07-13T13:57:43Z","groups":["Arrays"],"status":"ok","games":{"arma3":"0.58"},"since":"arma3 0.58","arma3_since":"0.58","description":"Sorts an array according to given algorithm. See sort for simple sorts.","syntaxes":[{"syntax":"[array, parameters, algorithm, direction, filter] call BIS_fnc_sortBy","params":[{"name":"array","type":"Array","description":"any unsorted array. Can contain any types","type_expr":"Array"},{"name":"parameters","type":"Array","description":"Array\n\n- used in the eval algorithm\n- input params are referenced in the sorting algorithm by _input0..9\n- a maximum of 10 input params is supported (0-9)","type_expr":"Array","text":"parameters: Array\n\n- used in the eval algorithm\n- input params are referenced in the sorting algorithm by _input0..9\n- a maximum of 10 input params is supported (0-9)"},{"name":"algorithm","type":"Code","optional":true,"default":"`{ _x }`","description":"needs to return String, Number or Array - see sort\n- _x refers to array item","type_expr":"Code","text":"algorithm: Code - (Optional, default `{ _x }`)\n\n- needs to return String, Number or Array - see sort\n- _x refers to array item"},{"name":"direction","type":"String","optional":true,"default":"\"ASCEND\"","description":"case-insensitive\n\n- \"ASCEND\" for ascending sorting\n- \"DESCEND\" (or anything else) for descending sorting","type_expr":"String","text":"direction: String - (Optional, default \"ASCEND\") case-insensitive\n\n- \"ASCEND\" for ascending sorting\n- \"DESCEND\" (or anything else) for descending sorting"},{"name":"filter","type":"Code","optional":true,"default":"`{ true }`","description":"code that needs to evaluate true for the array item to be sorted, otherwise item is removed","type_expr":"Code","text":"filter: Code - (Optional, default `{ true }`) code that needs to evaluate true for the array item to be sorted, otherwise item is removed"}],"returns":{"type":"Array","type_expr":"Array","description":"sorted array"}}],"examples":[{"code":["// sort numbers from lowest to highest\n_sortedNumbers = [[1,-80,0,480,15,-40], [], {_x}, \"ASCEND\"] call BIS_fnc_sortBy;"]},{"code":["// sort helicopters by distance from player\n_closestHelicopters = [[_heli1,_heli2,_heli3], [], { player distance _x }, \"ASCEND\"] call BIS_fnc_sortBy;"]},{"code":["// sort enemy by distance from friendly unit (referenced by local variable), the furthest first\n_furtherstEnemy = [[_enemy1,_enemy2,_enemy3], [_friendly], { _input0 distance _x }, \"DESCEND\", { canMove _x }] call BIS_fnc_sortBy;"]}],"see_also":["BIS_fnc_sortAlphabetically","BIS_fnc_sortNum","sort"]}