Description

This function creates a structure.

Returns

A structure. If value to be passed in the argument is "ordered", the function returns an ordered structure, which maintains the insertion order of structure elements.

Category

History

ColdFusion (2016 release) Update 3: Added support for sorted structs.

ColdFusion (2016 release): Added support for ordered structs.

Syntax

StructNew(structType, sortType, sortOrder, localeSensitive)
StructNew(structType,callback)
StructNew(structType)

See also

Structure functionsStructure functions in the Developing ColdFusion Applications guide.

Parameters

Parameter Description
structType (Optional) The type of struct to be created. This is new in Adobe ColdFusion (2016 release). You can specify either "Ordered" or leave structType blank.
sortType (Optional) Sort types are text or numeric.
sortOrder (Optional) Ascending ("asc") or descending ("desc").
localeSensitive (Optional) True or false.
callback (Optional) A comparator function that compares the keys and returns 1, 0, or -1.

Example

<cfscript>
       myStruct=StructNew("Ordered");
       myStruct.item1="Old Trafford";
       myStruct.item2="Anfield";
       myStruct.item3="Stamford Bridge";
       myStruct.item4="Villa Park";
       myStruct.item5="St James Park";
       myStruct.item6="Emirates Stadium";
       myStruct.item7="Etihad Stadium";
       WriteDump(myStruct);
</cfscript>

Output

Example struct
Example struct

Example with sortorder

<cfscript>
       // Create a struct of type ordered with sort type as text and sort order as ascending.
       someStruct=StructNew("ordered","text","asc",false);
       someStruct.jonas = {age=26, department="IT"};
       someStruct.jason= {age=29, department="Analytics"};
       someStruct.johnnie = {age=31, department="Accounting"};
       someStruct.john = {age=31, department="Audit"};
       WriteDump(someStruct);     
</cfscript>

Output

Example with sortorder
Example with sortorder

Example with callback

<cfscript>
sorted = structNew("ordered", function(e1value,e2value,e1key,e2key)
{
return compare(e1key,e2key);
});
       sorted.azure = "blue";
       sorted.adze = "tool";
       sorted.baffle = 01;
       sorted.adamantium = "dork";
       sorted.alabama = 3;
       sorted.ballad = 007;
       sorted.age = 36;
       sorted.aabc= "allardyce";
       sorted.baleful="hodgson";
       sorted.aardvark=-7;
       sorted.back="sort";
       writedump(sorted);
</cfscript>
Example with callback
Example with callback

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