Sorts a struct according to the specified arguments and returns an array of struct keys
Method Signature
StructSort(struct=[structloose], sortType=[any], sortOrder=[string], path=[string], callback=[function:Comparator])
Arguments
Argument
Type
Required
Description
Default
An optional sort type to apply to that type - if a callback is given in this position it will be used as that argument
The sort order applicable to the sortType argument
An optional callback to use as the sorting function. You can alternatively pass a Java Comparator.
Examples
Numeric sorting
Run Example
someStruct = {
RED : 93,
YELLOW : 90,
GREEN : 94
};
result = structSort( someStruct, "numeric", "desc" );
writeOutput( lcase( JSONSerialize( result ) ) );
Result: ["green", "red", "yellow"]
Sort by subelement
Run Example
someStruct = {};
someStruct.SCOTT = {
AGE : 26,
DEPARTMENT : "General"
};
someStruct.GLAN = {
AGE : 29,
DEPARTMENT : "computer"
};
someStruct.GEORGE = {
AGE : 31,
DEPARTMENT : "Physical"
};
result = structSort( someStruct, "textnocase", "asc", "department" );
writeOutput( lcase( JSONSerialize( result ) ) );
Result: ["glan","scott","george"]
Date sorting using callback
Compare values via dateCompare
birthdays = {
"Jim" : "1982/12/5",
"Anne" : "1968/9/13",
"Thomas" : "1975/3/28"
};
sorted = structSort( birthdays, ( Any e1, Any e2 ) => {
return dateCompare( e1, e2 );
} );
for( birthday in sorted ) {
writeOutput( birthday & " (" & dateDiff( "yyyy", birthdays[ birthday ], now() ) & "), " );
}
Result: Anne (49), Thomas (42), Jim (35),
Additional Examples
Run Example
animals = {
COW : {
TOTAL : 12
},
PIG : {
TOTAL : 5
},
CAT : {
TOTAL : 3
}
};
// Show current animals
Dump( label="Current animals", var=animals );
// Show animals sorted by total
Dump( label="Animals sorted by total", var=StructSort( animals, "numeric", "asc", "total" ) );
Run Example
myStruct = {
A : "London",
B : "Paris",
C : "Berlin",
D : "New York",
E : "Dublin"
};
// define callaback function
function callback( Any e1, Any e2 ) {
return compare( arguments.E1, arguments.E2 );
}
writeDump( var=myStruct, label="Before sorting" );
writeDump( var=StructSort( myStruct, callback ), label="After sorting" );