6 Replies Latest reply on Aug 9, 2010 9:12 AM by philmodjunk

    Button to alternate from Sort Ascending to Sort Descending

    Mitch

      Title

      Button to alternate from Sort Ascending to Sort Descending

      Post

      Hopefully this is a simple one....

      I have a list that displays related records from a portal.  Above each field row is the title text that has a button script attached.  On first click, the script will re-sort the records, ascending based on the related field.

      What I would like to do, is have the same button re-sort the records, descending on the second click, alternating each click.

      Mitch

        • 1. Re: Button to alternate from Sort Ascending to Sort Descending
          philmodjunk

          I'd use a global variable for this. If the variable is empty or = "Descending", sort the records in ascending order then set the variable to "Ascending". If the variable is = "Ascending", sort in descending order and set the variable to "Descending".

          That way your script will toggle the sort order each time it is performed.

          PS. "I have a list that displays related records from a portal."
          Are you displaying the record in a portal and then placing a button in that portal that changes the sort order? If so, I'd like to see how you've implemented that.

          • 2. Re: Button to alternate from Sort Ascending to Sort Descending
            Mitch

            Sorting within the portal would be better, but I believe impossible.

            Anyhow, thanks for the tip.  I will have to do some more reading.  I am not very confident with variables....

            Mitch

            • 3. Re: Button to alternate from Sort Ascending to Sort Descending
              philmodjunk

              You could also use a global field for this instead of a variable.

              • 4. Re: Button to alternate from Sort Ascending to Sort Descending
                Mitch

                Just on case anyone is wondering...Using PhilModJunk's suggestion.

                I created a global field 'gSortOrder'.  The Button script used the 'If' and 'Else' if function.

                Basically:

                If gSortOrder = Ascending

                Sort Descending

                Set Field gSortOrder = Descending

                Else If gSortOrder = Descending

                  Sort Ascending

                  Set Field gSortOrder = Ascending

                Else

                Sort Ascending

                  Set Field gSortOrder = Ascending

                End If

                Works fine. 

                • 5. Re: Button to alternate from Sort Ascending to Sort Descending
                  LaRetta_1

                  By using global variable, you don't need the global field.  And it is best not to create fields needlessly (they just clutter your field definitions).  If you use that global for other things, then that's fine to use it here but if not then a global variable is the way to go.  BTW, here's another way to write the script (and you can substitute the global field for the global variable if you really wish to):

                  If [ $$sort ]

                  Sort [ no dialog ; ascending ]

                  Else

                  Sort [ no dialog ; descending ]

                  End If

                  Set Variable [ $$sort ; not $$sort ]

                  ... what this means is that, if $$sort is set to 1, sort it ascending because it is already sorted descending; otherwise sort it ascending   descending (meaning $$sort is set to 0 or blank).  After the If[] test, the variable of $$sort is reversed (using a 0/1 boolean toggle).

                  This boolean $$sort will persist as long as the User is signed in.  When they first open the file, $$sort will be blank so activating this script will sort descending.  You can reverse the test if you wish to have the User's first button click to sort ascending instead.

                  • 6. Re: Button to alternate from Sort Ascending to Sort Descending
                    philmodjunk

                    LaRetta has posted exactly the script I had in mind.

                    There are trade offs between global fields vs. variables and developers each have their own preferences. My biggest beef with variables are that it can be a bit difficult to keep track of which ones are being used at a given time and simple typos in scripts can keep you from referencing the correct one and there's no error message to tell you that you "fat fingered" the variable name.

                    On the other hand, as LaRetta has said, using global fields like this add "clutter" to your table definitions. More and more, I'm finding that I'm moving such "clutter" out of the regular tables and defining them in a table dedicated just for global fields. That's worked well for all global fields not used in a relationship between table occurrences.

                    Also, referencing variables instead of global fields in some looping scripts can result in a script that performs a bit faster...