User Guide Cancel

StructToSorted

 

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)
StructToSorted(struct, sorttype, sortorder, localeSensitive)
StructToSorted(struct, sorttype, sortorder, localeSensitive)
<!--- Syntax with callback function--->
StructToSorted(struct,callback)
<!--- Syntax with callback function---> StructToSorted(struct,callback)
<!--- 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>
<cfscript> mystruct=StructNew(); mystruct.k1="one"; mystruct.k4="five"; mystruct.k2="three"; mystruct.k3="two"; mystr=StructToSorted(mystruct,"text","asc",false); writedump(mystr); </cfscript>
<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>
<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>
<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>
<cfscript> mystruct=StructNew(); mystruct.k1="one"; mystruct.k4="five"; mystruct.k2="three"; mystruct.k3="two"; mystr=mystruct.ToSorted("text","asc",false); writedump(mystr); </cfscript>
<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>
<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>
<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>

Get help faster and easier

New user?