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(struct [, sortType, sortOrder, path, localeSensitive]) structSort(struct, callback) → returns array

Member Function Syntax

struct.sort([sortType, sortOrder, path, localeSensitive])

structSort Argument Reference

struct struct
Required

A ColdFusion structure

sortType string
Default: text

* numeric
* text: case-sensitive
* textnocase
Values:
  • numeric
  • text
  • textnocase

sortOrder string
Default: asc

* asc: ascending (a to z) sort order.
* desc: descending (z to a) sort order
Values:
  • asc
  • desc

path string

Top-level key path; String or a variable that contains one

localeSensitive boolean
Default: false

CF 10+ Respect locale-specific characters (including support for umlaut characters) while sorting
(Applies to type"text" and "textnocase".

callback function

CF 2016+ A closure for sorting which takes two keys of the struct and returns whether the first value is greater than, equal to, or less than the second value. Inside compare function can be used (compare, compareNoCase, dateCompare or custom). function(key1, key2)

Compatibility

ColdFusion:

CF10+ Added support for locale-specific characters

Examples
Sample code invoking the structSort function

someStruct = {red=93,yellow=90,green=94};
result = structSort(someStruct, "numeric", "desc");
writeOutput( lcase(serializeJSON(result)) );

Expected Result: ["green", "red", "yellow"]

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(serializeJSON(result)) );

Expected Result: ["glan","scott","george"]

Compare values via dateCompare

birthdays = {
	'Jim': '1982/12/5',
	'Anne': '1968/9/13',
	'Thomas': '1975/3/28'
};

sorted = structSort(birthdays,function(e1,e2) {
	return dateCompare(e1,e2);
});

for(birthday in sorted) {
	writeOutput(birthday&' ('&dateDiff('yyyy',birthdays[birthday],now())&'), ');
}

Expected Result: Anne (49), Thomas (42), Jim (35),


Fork me on GitHub