2 Replies Latest reply on Apr 27, 2006 8:33 PM by Newsgroup_User

    using built in sortOn method

    DuhChamp
      I have an array of cards that I need to be sorted by rank. I am trying to use the sort on method, but I am not sure what parameter I should use.

      Here is what I have:

      handArray.sortOn("Card.getRank()"); //hand array holds the cards that need to be sorted, get rank returns the rank value

      I know this syntax is incorrect, but how would I do this using sort on, if it is possible? Another way I could do this would be to store the value of the rank into a new array, and then sort that array numerically, then swap the corresponding indexes of the handArray, but that seems inefficient.
        • 1. Re: using built in sortOn method
          DuhChamp Level 1
          Scratch this post. I just created a custom sorter function and used the sort method instead.
          • 2. Re: using built in sortOn method
            Level 7
            DuhChamp wrote:

            > I have an array of cards that I need to be sorted by rank. I am trying to
            > use the sort on method, but I am not sure what parameter I should use.
            >
            > Here is what I have:
            >
            > handArray.sortOn("Card.getRank()"); //hand array holds the cards that
            > need to
            > be sorted, get rank returns the rank value
            >
            > I know this syntax is incorrect, but how would I do this using sort on,
            > if it
            > is possible? Another way I could do this would be to store the value of
            > the rank into a new array, and then sort that array numerically, then swap
            > the corresponding indexes of the handArray, but that seems inefficient.

            sortOn makes a sort based on a property of the objects in your array. Unless
            you already have a property that can used to that effect, you could create
            a virtual property that uses your "getRank" method as the getter. Check out
            the method "addProperty" in "Object". Alternatively, create a custom sort
            function and use sort.

            Tim.