sort: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "\| *((\[\[[a-zA-Z0-9_ |()]+\]\],? ?)+) * \}\}" to "|seealso= $1 }}")
(formatting)
Line 1: Line 1:
{{RV|type=command
{{RV|type=command


| arma3
|game1= arma3


|1.44
|version1= 1.44


|gr1= Arrays
|gr1= Arrays


| Attempts to sort given array either in ascending ([[true]]) or descending ([[false]]) order. All array elements should be one of the following types:
|descr= Attempts to sort given array either in ascending ([[true]]) or descending ([[false]]) order.
* [[String]] - array of strings (["a","b","c"...])
* [[Number]] - array of numbers ([1,2,3...])
* [[Array]] - array of subarrays (<nowiki>[</nowiki>["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.
Mixed arrays (["a",1,[true]...]) are not supported and results are undefined.


| array '''sort''' order
|s1= array '''sort''' order


|p1= array: [[Array]]
|p1= array: [[Array]] - Array to be sorted, can also be a nested array.<br><br>
All array elements should be one of the following types:
* [[String]] - Array of strings {{ic|["a","b","c"...]}}
* [[Number]] - Array of numbers {{ic|[1,2,3...]}}
* [[Array]] - Array of subarrays {{ic|<nowiki>[</nowiki>["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.
Mixed arrays {{ic|["a",1,[true], ...]}} are not supported and results are undefined.<br><br>


|p2= order: [[Boolean]] - [[true]]: ascending, [[false]]: descending
|p2= order: [[Boolean]] - Sorting order.
* [[true]]: ascending
* [[false]]: descending


| [[Nothing]]
|r1= [[Nothing]]
   
   
|x1= <code>_arr = [5.21725,1.30859,4,5.03028,1];
|x1= <code>_arr = [5.21725,1.30859,4,5.03028,1];
Line 44: Line 47:
];</code>
];</code>


|seealso= [[set]], [[pushBack]], [[pushBackUnique]], [[apply]], [[select]], [[resize]], [[reverse]], [[count]], [[find]], [[in]], [[forEach]], [[deleteAt]], [[deleteRange]], [[append]], [[toArray]], [[toString]], [[param]], [[params]], [[arrayIntersect]], [[splitString]], [[joinString]], [[BIS_fnc_sortAlphabetically]], [[BIS_fnc_sortBy]], [[BIS_fnc_sortNum]]
|seealso= [[BIS_fnc_sortAlphabetically]] [[BIS_fnc_sortBy]] [[BIS_fnc_sortNum]]
}}
}}


{{GameCategory|arma3|Scripting Commands}}
<!-- CONTINUE Notes -->
<dl class="command_description">
<dl class="command_description">
<dd class="notedate">Posted on April 16, 2015 - 18:14 (UTC)</dd>
<dd class="notedate">Posted on April 16, 2015 - 18:14 (UTC)</dd>
Line 58: Line 57:
</dd>
</dd>
</dl>
</dl>
<!-- DISCONTINUE Notes -->

Revision as of 11:00, 28 March 2021

Hover & click on the images for description

Description

Description:
Attempts to sort given array either in ascending (true) or descending (false) order.
Groups:
Arrays

Syntax

Syntax:
array sort order
Parameters:
array: Array - Array to be sorted, can also be a nested array.

All array elements should be one of the following types:
  • String - Array of strings ["a","b","c"...]
  • Number - Array of numbers [1,2,3...]
  • 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.
Mixed arrays ["a",1,[true], ...] are not supported and results are undefined.

order: Boolean - Sorting order.
Return Value:
Nothing

Examples

Example 1:
_arr = [5.21725,1.30859,4,5.03028,1]; _arr sort true; hint str _arr; //[1,1.30859,4,5.03028,5.21725]
Example 2:
_dev = ["ja","pa","pa","tram","tara"]; _dev sort false; hint str _dev; //["tram","tara","pa","pa","ja"]
Example 3:
#define ASC true #define DESC false _scores = [[123,"bob",15],[123,"bill",20],[200,"dave",21],[200,"steve",11]]; _scores sort DESC; hint str _scores; //[[200,"steve",11],[200,"dave",21],[123,"bob",15],[123,"bill",20]]
Example 4:
Sort buildings by distance and return position of the most distant building:_buildings = player nearObjects ["Land_Cargo_Patrol_V1_F", 500]; _buildings = _buildings apply { [_x distance player, _x] }; _buildings sort false; hint format [ "Most distant building is at %1, distance %2 m", getPos (_buildings select 0 select 1), round (_buildings select 0 select 0) ];

Additional Information

See also:
BIS_fnc_sortAlphabetically BIS_fnc_sortBy BIS_fnc_sortNum

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
Posted on April 16, 2015 - 18:14 (UTC)
Killzone Kid
The algorithm for sorting subarrays: compare 1st element, if equal compare 2nd, if equal compare 3rd...etc.