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>

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy