3 Replies Latest reply on May 23, 2006 4:29 AM by DazFaz

    Array sort

    DazFaz Level 1
      Is there anyway that I can pass a particular string to an array and it return only the fields associated with that string. e.g.
      my_array.sort("age=50",Array.NUMERIC); In some way I want this to return ONLY the fields associated with all ages that are equal to 50 from within the array. I think Ive made that clear enough. Any help would be great.

      Thanks in advance

        • 1. Re: Array sort
          blemmo Level 1
          You could use Array.sortOn("age",Array.NUMERIC). This should sort the array, and you may check the first entries for their age value. But you would have to go through the whole array agin in the worst case, so it should be better to write a small function that returns an array containing all entries with age==50. Something like the attached (I assume there are objects in the array with an "age" property that has a Number assigned).

          hth,
          blemmo


          • 2. Re: Array sort
            Rothrock Level 5
            Maybe clear enough, but why does it have to be sort? Doesn't really have anything to do with sort does it?

            You want a find type of function or some such. Just make a function that does what you want. I'm guessing you don't want to destroy the original array so just step through the original array, compare what you want, put the ones that match in a new array, and return the array.

            Depending upon exactly what you are doing you could copy the entire element from the original array or just a reference to its position in the array.

            PS: You might want to use Array.sortOn() to make the searching easier.
            • 3. Re: Array sort
              DazFaz Level 1
              "Maybe clear enough, but why does it have to be sort? Doesn't really have anything to do with sort does it?"

              Not patronising at all is it. If I knew that then I would not be asking.

              I’ve been able to sort it out so thanks for everyone’s help with this one.