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

    structure of arrays sort

      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

          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

          • 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:

            session.cart.item = structnew();

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

            session.cart.item["mary"]["name"] = "mary";
            session.cart.item["mary"]["foodname"] = "Apple";
            session.cart.item["mary"]["price"] = "0.49";

            session.cart.item["adam"]["name"] = "adam";
            session.cart.item["adam"]["foodname"] = "Cake";
            session.cart.item["adam"]["price"] = "4.99";

            // 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"));

            <cfloop list="#nameList#" index="currentName">