Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop ColdFusion from sorting my structs/arrays

I have data represented in CF as an array of structs e.g.:

var foo = [{key = 'bar', value = 'baz', ... }...];

This structure gets iterated over sequentially and then translated to another related struct which looks like:

foo2[key] = {key = 'bar', value = 'baz', ...};

This is then sent to the SerializeJSON() method and sent to the browser. The problem is that the order of the keys in either foo or foo2 are alphabetical instead of in the order they were added. This is causing a problem on the client side as this collection is iterated over again and is expected to be ordered. Any suggestions?

like image 242
illvm Avatar asked Nov 21 '25 09:11

illvm


1 Answers

If your collection is expected to be ordered you need to use an array.

Structs don't guarantee any ordering, and shouldn't be used as such.

like image 86
DanSingerman Avatar answered Nov 25 '25 00:11

DanSingerman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!