Difference between revisions of "Ar CustomSort"
imported>QQuix (New page) |
imported>QQuix |
||
(2 intermediate revisions by the same user not shown) | |||
Line 5: | Line 5: | ||
The function should be defined to take two array_var arguments. When it is called, the arguments will contain exactly one element each - the elements to be compared. | The function should be defined to take two array_var arguments. When it is called, the arguments will contain exactly one element each - the elements to be compared. | ||
It should return true if the first argument is less than the second argument, and | It should return true if the first argument is less than the second argument, and false if it is greater than or equal to the second argument. | ||
You can define 'less', 'greater', and 'equal' in whatever way makes sense for you provided your definitions provide a definitive ordering of any set of values; otherwise the sort may never terminate. | You can define 'less', 'greater', and 'equal' in whatever way makes sense for you provided your definitions provide a definitive ordering of any set of values; otherwise the sort may never terminate. | ||
The optional third argument sorts the elements in reverse order. | The optional third argument sorts the elements in reverse order. | ||
===Notes=== | |||
*The returned array is always of type 'array'. | |||
*The original array may be of any type (array, map or stringmap). The original keys of map and stringmap arrays will be ignored. | |||
* As of OBSE 21, this function returns an empty array if the first element of the original array is an array. | |||
====Example==== | |||
In all three cases, the returned SortedArray will be exactly the same | |||
array_var as | |||
array_var ar | |||
array_var am | |||
array_var SortedArray | |||
let ar := ar_Construct array | |||
ar_Append ar 7 | |||
ar_Append ar 5 | |||
ar_Append ar 3 | |||
ar_Append ar 9 | |||
ar_Append ar 0 | |||
ar_Append ar 1 | |||
let SortedArray := ar_CustomSort ar zzCustomSort | |||
let am := ar_Construct map | |||
let am[1.1] := 7 | |||
let am[3] := 5 | |||
let am[4] := 3 | |||
let am[5.5] := 9 | |||
let am[6.6] := 0 | |||
let am[9] := 1 | |||
let SortedArray := ar_CustomSort am zzCustomSort | |||
let as := ar_Construct stringmap | |||
let as["A"] := 7 | |||
let as["B"] := 5 | |||
let as["C"] := 3 | |||
let as["D"] := 9 | |||
let as["E"] := 0 | |||
let as["F"] := 1 | |||
let SortedArray := ar_CustomSort as zzCustomSort | |||
scn zzCustomSort | |||
array_var ar1 | |||
array_var ar2 | |||
begin Function {ar1 ar2} | |||
if eval ar1[0] < ar2[0] | |||
SetFunctionValue 1 | |||
else | |||
SetFunctionValue 0 | |||
endif | |||
end | |||
| name = Ar_CustomSort | | name = Ar_CustomSort |
Latest revision as of 17:46, 2 March 2015
A function added by the Oblivion Script Extender.
Syntax:
(sorted:Array) Ar_CustomSort toSort:Array comparisonFunction:ref reverse:bool
Returns an Array sorted by calling the provided function script to perform comparison of elements.
The function should be defined to take two array_var arguments. When it is called, the arguments will contain exactly one element each - the elements to be compared.
It should return true if the first argument is less than the second argument, and false if it is greater than or equal to the second argument.
You can define 'less', 'greater', and 'equal' in whatever way makes sense for you provided your definitions provide a definitive ordering of any set of values; otherwise the sort may never terminate.
The optional third argument sorts the elements in reverse order.
Notes
- The returned array is always of type 'array'.
- The original array may be of any type (array, map or stringmap). The original keys of map and stringmap arrays will be ignored.
- As of OBSE 21, this function returns an empty array if the first element of the original array is an array.
Example
In all three cases, the returned SortedArray will be exactly the same
array_var as array_var ar array_var am array_var SortedArray let ar := ar_Construct array ar_Append ar 7 ar_Append ar 5 ar_Append ar 3 ar_Append ar 9 ar_Append ar 0 ar_Append ar 1 let SortedArray := ar_CustomSort ar zzCustomSort let am := ar_Construct map let am[1.1] := 7 let am[3] := 5 let am[4] := 3 let am[5.5] := 9 let am[6.6] := 0 let am[9] := 1 let SortedArray := ar_CustomSort am zzCustomSort let as := ar_Construct stringmap let as["A"] := 7 let as["B"] := 5 let as["C"] := 3 let as["D"] := 9 let as["E"] := 0 let as["F"] := 1 let SortedArray := ar_CustomSort as zzCustomSort
scn zzCustomSort array_var ar1 array_var ar2 begin Function {ar1 ar2} if eval ar1[0] < ar2[0] SetFunctionValue 1 else SetFunctionValue 0 endif end