Whatever message this page gives is out now! Go check it out!

StructSort

Last update:
May 18, 2026

Description

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.

Returns

An array of top-level key names (strings), sorted by the value of the specified sub-element.

Category

Syntax

StructSort(struct [, sortType[, sortOrder[, path[, localeSensitive]]]])
Using callback:
StructSort(struct,callback)

See also

Structure functionsStructure functions in the Developing ColdFusion Applications

Parameters

Parameter
Description
struct
A ColdFusion structure
sortType
  • numeric
  • text: case sensitive (all lowercase letters precede the first uppercase letter). Default.
  • textnocase
sortOrder
  • asc: ascending (a to z) sort order. Default.
  • desc: descending (z to a) sort order
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.

Usage

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 ".

Example

<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>

Output

["c","e","a","d","b"]

Using member function

<cfscript>
       myStruct={a="London",b="Paris",c="Berlin",d="New York",e="Dublin"};
       myNewSort=myStruct.sort("text","asc");
       WriteDump(myNewSort);
</cfscript>
Example using callback
<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>

Share this page

Was this page helpful?
We're glad. Tell us how this page helped.
We're sorry. Can you tell us what didn't work for you?
Thank you for your feedback. Your response will help improve this page.

On this page