3 Replies Latest reply on May 6, 2009 9:07 AM by kglad

    How to order elements of an array?

    Dardo99

      I´m trying to order elements of an array. When the dates come from dinamics text it´s no problem, bad when come from input text the dates are treated like strings and it´s imposible to order correctly, because 15 is considered less than 3. In both cases I can operate correctly whit this dates like numbers.¿How is possible to order this dates from input text, like numbers?

      Thank´s

        • 1. Re: How to order elements of an array?
          kglad Adobe Community Professional & MVP

          check the sort() method of arrays.  you can create custom sort functions and, in your situation, sort array elements like numbers using a parameter passed to the sort() method:

           

          yourArray.sort(Array.NUMERIC);

          • 2. Re: How to order elements of an array?
            Dardo99 Level 1

            Thank´s

            I have already done that but only works with values that i write directly in the matrix, but not with those from an input field.

            Part of script used is as follows:

             

             

             

            var my=[dato11,dato21,dato31,dato41];

            If I enter de values 3,5,15,7,8 in the appropiate input fields, when I write my.sort(Array.NUMERIC);then result is: 15,3,5,7,8

            If I write my=[3,5,15,7,8]; it works correctly.

            I d´nt know why

            Greetings

            • 3. Re: How to order elements of an array?
              kglad Adobe Community Professional & MVP

              values from textfields are strings (even if it looks like a number to you).  there are several ways to handle this and one is to cast that string as a number before you add it to your array:

               

              my.push(Number(yourtextfield.text));