Parameter
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.
An array of top-level key names (strings), sorted by the value of the specified sub-element.
StructSort(struct [, sortType[, sortOrder[, path[, localeSensitive]]]])
Using callback:
StructSort(struct,callback)
Structure functions; Structure functions in the Developing ColdFusion Applications
Parameter |
Description |
---|---|
struct |
A ColdFusion structure |
sortType |
|
sortOrder |
|
path |
String or a variable that contains one. Path to apply to each top-level key, to reach element value by which to sort. The default value is nothing (top-level entries sorted by their own values). |
localeSensitive |
Specify if you wish to do a locale-sensitive sorting. The default value is false. |
callback |
A function which takes two keys of the struct, and returns whether the first value if greater than, equal to, or less than the second value. |
The path string does not support array notation, and only supports substructures of structures.This function does not sort or change the structure. In ColdFusion 10, added support for all Java supported locale-specific characters (including support for umlaut characters). A flag for this support has been added for sorttype = "text" or sorttype = " textnocase ".
<cfscript> myStruct=StructNew(); myStruct={a="London",b="Paris",c="Berlin",d="New York",e="Dublin"}; mySort=StructSort(myStruct,"text","asc"); //Sorts the top level keys in the struct in ascending order WriteOutput(serializeJSON(mySort)); </cfscript>
["c","e","a","d","b"]
<cfscript> myStruct={a="London",b="Paris",c="Berlin",d="New York",e="Dublin"}; myNewSort=myStruct.sort("text","asc"); WriteDump(myNewSort); </cfscript>
<cfscript> myStruct=StructNew(); myStruct={a="London",b="Paris",c="Berlin",d="New York",e="Dublin"}; // define callaback function function callback(e1, e2){ return compare(e1, e2); } mySort=StructSort(myStruct,callback); writedump(mySort); </cfscript>