StructNew

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 (2021 release): Introduced the following types of structs to be created:

  • ordered-casesensitive
  • casesensitive

ColdFusion (2018 release): Introduced named parameters.

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

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

Syntax

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

See also

Structure functionsStructure functions in the Developing ColdFusion Applications guide.

Parameters

Parameter

Description

type

(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

Example- case sensitive struct

<cfscript> 
    animals=StructNew("casesensitive") 
    animals.Aardwolf="Proteles cristata" 
    animals.Aardvark="Orycteropus afer" 
    animals.Alligator="Mississippiensis" 
    animals.Albatross="Diomedeidae" 
    writeDump(animals) 
</cfscript>

Output

cs1

Example 2- Case sensitive struct

<cfscript> 
    animals=StructNew("casesensitive") 
    animals.Aardwolf="Proteles cristata" 
    animals.aardvark="Orycteropus afer" 
    animals.Alligator="Mississippiensis" 
    animals.albatross="Diomedeidae" 
    writeDump(animals) 
</cfscript>

Output

cs2

Example- Ordered case sensitive struct

<cfscript> 
    animals=StructNew("ordered-casesensitive") 
    animals.Aardwolf="Proteles cristata" 
    animals.Aardvark="Orycteropus afer" 
    animals.Alligator="Mississippiensis" 
    animals.Albatross="Diomedeidae" 
    writeDump(animals) 
</cfscript>

Output

ocs1

Example 2- Ordered case sensitive struct

<cfscript> 
    animals=StructNew("ordered-casesensitive") 
    animals.Aardwolf="Proteles cristata" 
    animals.aardvark="Orycteropus afer" 
    animals.alligator="Mississippiensis" 
    animals.Albatross="Diomedeidae" 
    writeDump(animals) 
</cfscript>

Output

ocs2

 Adobe

Get help faster and easier

New user?