{"name":"sort","title":"sort","type":"command","url":"https://community.bistudio.com/wiki/sort","revid":377104,"updated":"2026-01-01T10:00:38Z","groups":["Arrays"],"status":"ok","games":{"arma3":"1.44"},"since":"arma3 1.44","arma3_since":"1.44","description":"Attempts to sort given array either in ascending (true) or descending (false) order.\n\n> [!IMPORTANT]\n> This will **modify** the original array!","syntaxes":[{"syntax":"array sort order","params":[{"name":"array","type":"Array of Anything","description":"array to be sorted, can also be a nested array.\n\nAll array elements should be one of the following types:\n\n- String - Array of strings `[\"a\",\"b\",\"c\"...]`\n- Number - Array of numbers `[1,2,3...]`\n- Array - Array of subarrays `[[\"a\",1,2],[\"b\",3,4],[\"c\",5,6]...]`. Subarrays should be of the same structure. Subarray elements other than String or Number will be ignored during sorting.\n\nMixed arrays `[\"a\",1,[true], ...]` are not supported and results are undefined.","type_expr":"Array<Anything>"},{"name":"order","type":"Boolean","description":"sorting order.\n\n- true: ascending\n- false: descending","type_expr":"Boolean"}],"returns":{"type":"Nothing","type_expr":"Nothing"},"form":"binary","left":{"text":"array"},"right":{"text":"order"}}],"examples":[{"code":["_arr = [5.21725,1.30859,4,5.03028,1];\n_arr sort true;\nhint str _arr; //[1,1.30859,4,5.03028,5.21725]"]},{"code":["_dev = [\"ja\",\"pa\",\"pa\",\"tram\",\"tara\"];\n_dev sort false;\nhint str _dev; //[\"tram\",\"tara\",\"pa\",\"pa\",\"ja\"]"]},{"code":["#define ASC true\n#define DESC false\n_scores = [[123,\"bob\",15],[123,\"bill\",20],[200,\"dave\",21],[200,\"steve\",11]];\n_scores sort DESC;\nhint str _scores; //[[200,\"steve\",11],[200,\"dave\",21],[123,\"bob\",15],[123,\"bill\",20]]"]},{"text":"Sort buildings by distance and return position of the most distant building:\n\n```sqf\n_buildings = player nearObjects [\"Land_Cargo_Patrol_V1_F\", 500];\n_buildings = _buildings apply { [_x distance player, _x] };\n_buildings sort false;\nhint format [\n    \"Most distant building is at %1, distance %2 m\",\n    getPos (_buildings select 0 select 1),\n    round (_buildings select 0 select 0)\n];\n```","code":["_buildings = player nearObjects [\"Land_Cargo_Patrol_V1_F\", 500];\n_buildings = _buildings apply { [_x distance player, _x] };\n_buildings sort false;\nhint format [\n\t\"Most distant building is at %1, distance %2 m\", \n\tgetPos (_buildings select 0 select 1),\n\tround (_buildings select 0 select 0)\n];"]}],"see_also":["lbSortBy","lnbSortBy","BIS_fnc_sortAlphabetically","BIS_fnc_sortBy","BIS_fnc_sortNum"],"notes":[{"user":"Killzone_Kid","date":"2015-04-16T18:14:00Z","text":"The algorithm for sorting subarrays: compare 1st element, if equal compare 2nd, if equal compare 3rd...etc."}]}