StructSort()
Returns a sorted array of the top level keys in a structure.
Sorts using alphabetic or numeric sorting, and can sort based on the values of any structure element.
StructSort( base=struct, sortTypeOrSortFunc=any, sortOrder=string, pathToSubElement=string );
Returns: Array
| Argument | Description | Default |
|---|---|---|
|
base
struct,
required
|
edit
A structure with one field (an associative array). |
|
|
sortTypeOrSortFunc
any,
optional
|
edit
Define one of the following:
Alias: sortType |
text |
|
sortOrder
string,
optional
|
edit
sort direction:
|
asc |
|
pathToSubElement
string,
optional
|
edit
String or a variable that contains one Alias: path |
Examples
editNon-Member Function
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")
);
Example using callback (Introduced in 6.2.1.29)
myStruct={a="London",b="Paris",c="Berlin",d="New York",e="Dublin"};
// define callaback function
function callback(e1, e2){
return compare(arguments.e1, arguments.e2);
}
writeDump(var=myStruct,label="Before sorting")
writeDump(var=StructSort(myStruct,callback),label="After sorting");
Related System Properties / Environment Variables
- LUCEE_SECURITY_LIMITEVALUATION - Disable evaluating expressions when accessing scopes, Lucee 7 defaults to enabling this setting for security reasons. A boolean value. If enabled, limits variable evaluation in functions/tags. If enabled, you cannot use expressions within [ ] like this: susi[getVariableName()]. This affects the following functions: IsDefined, structGet, structSort (when using pathToSubElement), empty and the following tags: savecontent attribute `variable`
Type: boolean, Default: false (Lucee 6), true (Lucee 7)
See also
- Structures
- Sorting
- struct.sort()
- Search Issue Tracker open_in_new
- Search Lucee Test Cases open_in_new (good for further, detailed examples)