sort: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
(ver)
m (Text replacement - "<sqf>([^↵][^<]*↵[^<]*)<\/sqf>" to "<sqf> $1 </sqf>")
 
(57 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{Command|= Comments
{{RV|type=command
____________________________________________________________________________________________


| arma3dev |= Game name
|game1= arma3
|version1= 1.44


|1.44|= Game version
|gr1= Arrays


____________________________________________________________________________________________
|descr= Attempts to sort given array either in ascending ([[true]]) or descending ([[false]]) order. {{Feature|important|This will '''modify''' the original array!}}


| Attempts to sort given array either in ascending ([[true]]) or descending ([[false]]) order. All array elements should be one of the following types:
|s1= array [[sort]] 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. |= Description
____________________________________________________________________________________________


| array '''sort''' order|= Syntax
|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 {{hl|["a","b","c"...]}}
* [[Number]] - Array of numbers {{hl|[1,2,3...]}}
* [[Array]] - Array of subarrays {{hl|[<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 {{hl|["a",1,[true], ...]}} are not supported and results are undefined.<br><br>


|p1= array: [[Array]] |= Parameter 1
|p2= order: [[Boolean]] - sorting order.
* [[true]]: ascending
* [[false]]: descending


|p2= order: [[Boolean]] - [[true]]: ascending, [[false]]: descending |= Parameter 2
|r1= [[Nothing]]


| [[Nothing]] |= Return value
|x1= <sqf>
____________________________________________________________________________________________
_arr = [5.21725,1.30859,4,5.03028,1];
 
_arr sort true;
|x1= <code>_arr = [5.21725,1.30859,4,5.03028,1];
hint str _arr; //[1,1.30859,4,5.03028,5.21725]
_arr [[sort]] [[true]];
</sqf>
[[hint]] [[str]] _arr; //[1,1.30859,4,5.03028,5.21725]</code> |= Example 1


|x2= <code>_dev = ["ja","pa","pa","tram","tara"];
|x2= <sqf>
_dev [[sort]] [[false]];
_dev = ["ja","pa","pa","tram","tara"];
[[hint]] [[str]] _dev; //["tram","tara","pa","pa","ja"]</code> |= Example 2
_dev sort false;
hint str _dev; //["tram","tara","pa","pa","ja"]
</sqf>


|x3= <code><nowiki>#</nowiki>define ASC true
|x3= <sqf>
<nowiki>#</nowiki>define DESC false
#define ASC true
_scores = <nowiki>[</nowiki>[123,"bob",15],[123,"bill",20],[200,"dave",21],[200,"steve",11]];
#define DESC false
_scores [[sort]] DESC;
_scores = [[123,"bob",15],[123,"bill",20],[200,"dave",21],[200,"steve",11]];
[[hint]] [[str]] _scores; //<nowiki>[</nowiki>[200,"steve",11],[200,"dave",21],[123,"bob",15],[123,"bill",20]]</code> |= Example 2
_scores sort DESC;
hint str _scores; //[[200,"steve",11],[200,"dave",21],[123,"bob",15],[123,"bill",20]]
</sqf>


 
|x4= Sort buildings by distance and return position of the most distant building:
|x4= Sort buildings by distance and return position of the most distant building:<code>_buildings = [[player]] [[nearObjects]] ["Land_Cargo_Patrol_V1_F", 500];
<sqf>
{
_buildings = player nearObjects ["Land_Cargo_Patrol_V1_F", 500];
_buildings [[set]] [_forEachIndex, [_x [[distance]] [[player]], _x]];
_buildings = _buildings apply { [_x distance player, _x] };
} [[forEach]] _buildings;
_buildings sort false;
_buildings [[sort]] [[false]];
hint format [
[[hint]] [[format]] [
"Most distant building is at %1, distance %2 m",  
"Most distant building is at %1, distance %2 m",  
[[getPos]] (_buildings [[select]] 0 [[select]] 1),
getPos (_buildings select 0 select 1),
[[round]] (_buildings [[select]] 0 [[select]] 0)
round (_buildings select 0 select 0)
];</code>|= Example 4
];
</sqf>


____________________________________________________________________________________________
|seealso= [[lbSortBy]] [[lnbSortBy]] [[BIS_fnc_sortAlphabetically]] [[BIS_fnc_sortBy]] [[BIS_fnc_sortNum]]
 
}}
| [[set]], [[pushBack]], [[resize]], [[reverse]], [[count]], [[find]], [[in]], [[forEach]], [[deleteAt]], [[deleteRange]], [[append]], [[toArray]], [[toString]], [[param]], [[params]], [[BIS_fnc_sortAlphabetically]], [[BIS_fnc_sortBy]], [[BIS_fnc_sortNum]] |= See also


{{Note
|user= Killzone_Kid
|timestamp= 20150416181400
|text= The algorithm for sorting subarrays: compare 1st element, if equal compare 2nd, if equal compare 3rd...etc.
}}
}}
[[Category:Scripting_Commands_Arma_3]]
[[Category:Command_Group:_Variables|{{uc:{{PAGENAME}}}}]]
<!-- CONTINUE Notes -->
<dl class="command_description">
<dd class="notedate">Posted on April 16, 2015 - 18:14 (UTC)</dd>
<dt class="note">[[User:Killzone Kid|Killzone Kid]]</dt>
<dd class="note">
The algorithm for sorting subarrays: compare 1st element, if equal compare 2nd, if equal compare 3rd...etc.
</dd>
</dl>
<!-- DISCONTINUE Notes -->

Latest revision as of 19:42, 3 September 2024

Hover & click on the images for description

Description

Description:
Attempts to sort given array either in ascending (true) or descending (false) order.
This will modify the original array!
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:
lbSortBy lnbSortBy 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
Killzone_Kid - c
Posted on Apr 16, 2015 - 18:14 (UTC)
The algorithm for sorting subarrays: compare 1st element, if equal compare 2nd, if equal compare 3rd...etc.