Search code examples
javaspringblazedsflex4.5

How do I keep my collection in sorted order when sent as ArrayCollection and received as java.util.Set on the server?


I've developed a web application using Flex 4.5, Java, BlazeDS and Spring.

I just discovered that when I fetch a collection (java.util.Set) from the server as an ArrayCollection, sort the ArrayCollection in the Flex web application and send it back to the server, this collection is received by the server as unsorted java.util.Set.

Any suggestion on how I can keep the collection sorted when sent to the server?


Solution

  • You'll need to use an object on the AS side and a Map on the Java side. In AS, put all the elements from your arraycollection in one object like this

                var list:ArrayCollection = new ArrayCollection();
                var obj:Object = new Object();
                list.addItem("test");
                list.addItem("test2");
                for(var i=0;i<list.length;i++){
                    obj[i] = list.getItemAt(i);
                }
    

    After that , send the obj to the Java side - an AS object will be serialized to a Java Map. On the server side you will have the positions as the keys from your map, so you will be able to rebuild the order (the Java code is trivial, so I'm not going to write it down here).