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

StructToSorted

Last update:
May 18, 2026

Description

Converts any struct to a sorted struct.
Note:
If the input struct has an inner struct, StructToSorted does not sort the inner struct. StructToSorted only sorts structs at the first level.

Returns

A structure.

Category

History

ColdFusion (2018 release): Introduced named parameters.
ColdFusion (2016 release) Update 3: Added the function.

Syntax

StructToSorted(struct, sorttype, sortorder, localeSensitive)
<!--- Syntax with callback function--->

StructToSorted(struct,callback)

Parameters

Parameter
Description
struct
The structure to be sorted.
sortType
Sort types are text or numeric. Text value produces a case-insensitive sort.
sortOrder
Ascending (" asc ") or descending ("desc").
localeSensitive
True or false.
callback
The callback function to be used. The function compares the values in a struct and returns -1,0, and 1 depending on the values.

Example

<cfscript>
 mystruct=StructNew();
 mystruct.k1="one";
 mystruct.k4="five";
 mystruct.k2="three";
 mystruct.k3="two";
 mystr=StructToSorted(mystruct,"text","asc",false);
 writedump(mystr);
</cfscript>

Example with callback function

<cfscript>
 mystruct=StructNew();
 mystruct.k1="one";
 mystruct.k4="five";
 mystruct.k2="three";
 mystruct.k3="two";
 
 callback=function(value1,value2,key1,key2)
 {
  if (key1>key2)
   return 1;
  else
   return -1;
 };
 mystr=StructToSorted(mystruct,callback);
 writedump(mystr);
</cfscript>

Using member function

<cfscript>
 mystruct=StructNew();
 mystruct.k1="one";
 mystruct.k4="five";
 mystruct.k2="three";
 mystruct.k3="two";
 
        mystr=mystruct.ToSorted("text","asc",false);
 writedump(mystr);
</cfscript>

Using member function with callback

<cfscript>
 mystruct=StructNew();
 mystruct.k1="one";
 mystruct.k4="five";
 mystruct.k2="three";
 mystruct.k3="two";
 
 callback=function(value1,value2,key1,key2)
 {
  if (key1>key2)
   return 1;
  else
   return -1;
 };
 mystr=mystruct.ToSorted(callback);
 writedump(mystr);
</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