views:

286

answers:

1

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?

+10  A: 

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.

DanSingerman
Instead of using an array you could also keep the struct and include an array with the struct keys in the required order. That way you could have both predictable iteration *and* quick access object by key.
Tomalak
or try: http://java.sun.com/javase/6/docs/api/java/util/LinkedHashMap.html
Henry