Difference between revisions of "Ar CustomSort"

From the Oblivion ConstructionSet Wiki
Jump to navigation Jump to search
imported>QQuix
(Added notes and example)
imported>QQuix
 
(One intermediate revision by the same user not shown)
Line 15: Line 15:
*The returned array is always of type 'array'.
*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.
*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====
====Example====

Latest revision as of 17:46, 2 March 2015

< [[::Category:Functions|Category:Functions]]

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


See Also