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, sorttype, sortorder, pathtosubelement, localeSensitive) structSort(base, callback) → returns array

structSort Argument Reference

base struct
Required

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

pathtosubelement 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/n(Applies to type"text" and "textnocase".

callback function

Using a closure for sorting. Inside compare function can be used (compare, compareNoCase, dateCompare or custom).

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