StructToSorted()

Returns a struct sorted by the top level keys in a structure.

Introduced: 6.2.1.6

StructToSorted( base=struct, sortTypeOrSortFunc=any, sortOrder=string, localeSensitive=boolean );

Returns: Struct

Argument Description Default
base
struct, required

A structure with one field (an associative array).

sortTypeOrSortFunc
any, optional

The following sort types are available:

  • numeric: sorts numbers
  • text: sorts text alphabetically, taking case into account (case sensitive, default)
  • textNoCase: sorts text alphabetically, without regard to case (case insensitive)

or a function to be used as a comparator (introduced 6.2.1)

function(key1, key2)

Alias: sortType

text

sortOrder
string, optional

Sort direction:

  • asc (default): ascending (a to z) sort order
  • desc: descending (z to a) sort order

asc

localeSensitive
boolean, optional

Use locale aware sort

false

Examples

Non-Member Function

myNestedStruct = {
        "apple" = { "price" = 3, "quantity" = 10 },
        "banana" = { "price" = 1, "quantity" = 5 },
        "cherry" = { "price" = 2, "quantity" = 8 },
        "date" = { "price" = 4, "quantity" = 12 }
    };
   writeDump(var=myNestedStruct,label="Before sorting");
   writeDUmp(var=StructToSorted(myNestedStruct,"textNoCase","desc",true),label="After sorting");

Example using callback

myNumb.4="5";
    myNumb.3="2";
    myNumb.2="3";
    myNumb.1="1";
    cb = function( value1, value2, key1, key2 ){
        if (arguments.value1 < arguments.value2 ) // i.e. desc
            return 1;
        else
            return -1;
    };
    writeDump(var=myNumb, label="Befor sorting");
    writeDUmp(var=StructToSorted( myNumb , cb ), label="After sorting");

See also