{"name":"sort","operator":null,"title":"sort","type":"command","url":"https://community.bistudio.com/wiki/sort","revid":377104,"updated":"2026-01-01T10:00:38Z","groups":["Arrays"],"status":"ok","replaced_by":[],"games":[{"game":"arma3","name":"Arma 3","version":"1.44"}],"since":{"game":"arma3","name":"Arma 3","version":"1.44"},"arma3_since":"1.44","branch":null,"locality":{"argument":null,"effect":null,"server_exec":false},"exec":null,"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","since":null,"effect":null,"exec":null,"params":[{"name":"array","type":"Array of Anything","optional":false,"default":null,"description":"array to be sorted, can also be a nested array.\n\nAll array elements should be one of the following types:\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.\nMixed arrays `[\"a\",1,[true], ...]` are not supported and results are undefined.","types":["Array","Anything"],"since":null,"text":"array: Array of Anything - array to be sorted, can also be a nested array.\n\nAll array elements should be one of the following types:\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.\nMixed arrays `[\"a\",1,[true], ...]` are not supported and results are undefined."},{"name":"order","type":"Boolean","optional":false,"default":null,"description":"sorting order.\n- true: ascending\n- false: descending","types":["Boolean"],"since":null,"text":"order: Boolean - sorting order.\n- true: ascending\n- false: descending"}],"returns":{"type":"Nothing","types":["Nothing"],"description":null,"text":"Nothing"},"form":"binary","operator":null,"left":{"text":"array","types":["Array","Anything"]},"right":{"text":"order","types":["Boolean"]}}],"examples":[{"text":"```sqf\n_arr = [5.21725,1.30859,4,5.03028,1];\n_arr sort true;\nhint str _arr; //[1,1.30859,4,5.03028,5.21725]\n```","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]"]},{"text":"```sqf\n_dev = [\"ja\",\"pa\",\"pa\",\"tram\",\"tara\"];\n_dev sort false;\nhint str _dev; //[\"tram\",\"tara\",\"pa\",\"pa\",\"ja\"]\n```","code":["_dev = [\"ja\",\"pa\",\"pa\",\"tram\",\"tara\"];\n_dev sort false;\nhint str _dev; //[\"tram\",\"tara\",\"pa\",\"pa\",\"ja\"]"]},{"text":"```sqf\n#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]]\n```","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];"]}],"multiplayer":null,"problems":null,"aliases":[],"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."}],"categories":["Arma 3: New Scripting Commands","Arma 3: Scripting Commands","Command Group: Arrays","Introduced with Arma 3 version 1.44","Scripting Commands"]}