2 Replies Latest reply on May 29, 2006 3:56 AM by Fernis

    structure of arrays sort

    sodapopinski
      Hi, I have a structure of arrays as follows.
      The Structure is session.cart
      my arrays are session.cart.foodname[ ]
      session.cart.comments[ ]
      session.cart.name[ ]
      session.cart.price[ ]

      All of these arrays correspond to each other, so session.cart.name[1] may be John, who ordered a cheeseburger (session.cart.foodname[1]). So i need to sort these by the persons name and have the other 3 arrays sort with them, so they still correspond to each other. Can anyone help me with this? Ive looked over the structsort function but cant figure out how to apply it to a structure of arrays. Thanks for the help.
        • 1. Re: structure of arrays sort
          Level 7
          > All of these arrays correspond to each other, so session.cart.name[1] may be
          > John,

          Why would more than one person have a cart in the same session?

          Sounds to me like the logic behind your data structure definition might
          need revision.

          Can you give some sample data, and more of an explanation of the data
          structure?

          It's going to be *very difficult* to apply the sorting that you want to the
          structure you have, which - to me - means the structure is wrong for your
          requirements.

          --
          Adam
          • 2. Re: structure of arrays sort
            Fernis Level 3
            I would avoid using multiple arrays, which have to refer to each other - without really having a proper way to execute the plan. Structures are better for this. I created an example, which you might try out, and see if it fits your needs:

            <cfscript>
            session.cart.item = structnew();

            // use the name also as a key
            session.cart.item["john"]=structnew();
            session.cart.item["john"]["name"] = "john";
            session.cart.item["john"]["foodname"] = "Ribs";
            session.cart.item["john"]["price"] = "13.99";
            session.cart.item["john"]["comments"]="Johncomment";

            session.cart.item["mary"]=structnew();
            session.cart.item["mary"]["name"] = "mary";
            session.cart.item["mary"]["foodname"] = "Apple";
            session.cart.item["mary"]["price"] = "0.49";
            session.cart.item["mary"]["comments"]="Marycomment";

            session.cart.item["adam"]=structnew();
            session.cart.item["adam"]["name"] = "adam";
            session.cart.item["adam"]["foodname"] = "Cake";
            session.cart.item["adam"]["price"] = "4.99";
            session.cart.item["adam"]["comments"]="Adamcomment";

            // the following line actually does all you ever wanted. arrayToList() is just a bonus
            // for easy-to-understand-output in the end. More elegant solutions may exist. :-)

            nameList = arrayToList(structSort(session.cart.item,"textnocase","asc","name"));
            </cfscript>

            <cfoutput>
            <cfloop list="#nameList#" index="currentName">
            #session.cart.item[currentName].name#<BR>
            #session.cart.item[currentName].foodname#<BR>
            #session.cart.item[currentName].price#<BR>
            #session.cart.item[currentName].comments#<BR>
            <HR>
            </cfloop>
            </cfoutput>