Key order in ColdFusion structures

December 10, 2010 by Jean

One issue with ColdFusion structures is that they do not preserve the order of their keys. Considering:

variables.myStruct = structNew(); = 1;
variables.myStruct.two = 2;

The output of:


is not always the same. Sometimes "one,two" and sometimes "two,one". It depends on server conditions that only unicorns understand. For the rare cases where the order of the keys does matter, the cure is to use a Java hash map:

variables.myStruct = createObject("java", "java.util.LinkedHashMap").init(); = 1;
variables.myStruct.two = 2;

The output of #structKeyList(variables.myStruct)# will always be "one,two" - the order in which the keys have been created.


  • Most (if not all) structure functions (structKeyList(), structKeyExists() ...) can be applied to the Java hash maps. YAY!
  • A cfdump of a Java hash map does not respect the order of the keys, but seems to order the keys alphabetically.
  • To be tested, but it looks like Railo preserve the order of keys in structures.

In: ColdFusion 

Comment by Dominique Jacques-Brissette

July 22, 2014 at 11:47 AM

This was immensely useful, I didn't understand how the coldfusion struct key order was supposed to work.. but now I know why : I am NOT a unicorn!

To preserve the order of a file I edit I was going to change my code significantly... but using the java.util.LinkedHashMap only required me to change 1 line of code (the initialization of the struct)! THANK YOU SO MUCH!

Comment by Jean

July 22, 2014 at 12:02 PM

Glad it helped. The article is 4 years old, and the following syntax is now supported by Railo:

myStruct = structNew('linked');

Which is even easier.

Comment by Dominique Jacques-Brissette

July 22, 2014 at 12:05 PM

I do not use Railo, but that is good to know!

By the way, in the 'New comment' email I just received from this website, the link has a bad URL, I thought I would let you know, it looks like this : http://jean.*/notes/key-order-in-coldfusion-structures.

And when I click it, it displays as such in my browser : http://jean.%2A/notes/key-order-in-coldfusion-structures

Comment by Martin Parry

September 20, 2015 at 12:20 AM

Thanks soooooo much for this - I was having a problem with serializing and deserialzing JSON where the order of fields were'nt being preserved - That, along with using and a few modifications to where it used StructNew() to use your hashmap solved the problem.

I have posted a bug on adobe bug tracker and mentioned this page..

Many thanks