4 Replies Latest reply on Oct 7, 2015 7:55 PM by IanRenwick

    Sorting multiple fields Up/Down using one script.

    KylePutzier

      I have many layouts that are basically a list view with buttons that are used as column headers. When a column header button is pushed, it sorts the field in that column. This done simply by sending a script param that identifies which column header button is pushed and the script If/Else's until it finds a match. It then sorts on the appropriate field. It is currently only able to sort in one direction only. Users expect to toggle the sort order (ascending/decending) each time the column header button is pressed. GetNthRecord can tell me which order it is currently sorted in. I can use brute force to make this happen, but I have a lot of fields and a lot of list view layouts. It would be a lot of work.

       

      I would like to pass the name of the field to a script and have it set a variable to something I can test on to determine if the value of the first record of that field is greater than the value of the last record in the found set.

       

      Or, if there is a better way, please tell.

       

      Kyle

        • 1. Re: Sorting multiple fields Up/Down using one script.
          brsamuel

          I have always used global variables for this "feature".

           

          ( I didn't intend to leave such a terse reply - I got pulled away from my desk.)

           

          I store the Table Occurance (or layout name), field name, and sort direction in global vars that are referenced and set by the sort script. 

          There are more elegant ways to accomplish the effect, but this is simple & straightforward.

           

          Marc

           

          Message was edited by: brsamuel

          • 2. Re: Sorting multiple fields Up/Down using one script.
            raybaudi

            If you are on v 12, it is very easy.

             

            No need of script parameters, neither buttons...

            only the real fields in the header, each one using an OnObjectEnter trigger that fires this script:

            script.png

            • 3. Re: Sorting multiple fields Up/Down using one script.
              KylePutzier

              Both suggestions from Marc and Daniele were helpful.

               

              I have 20...30 layouts, each with 5...10 fields that look similar to Mac OS X column view. I want to be able to sort them just like OS X does. Click the column header and the corresponding column alternately sorts ascending or descending.

               

              This is what I will end up doing (most likely).

              I have a header button that calls the script below. It passes a value that corresponds to the Object name of the field I want to sort. It uses a repeating variable to remember the sort order of all the fields.

               

              And the best part, it's unbelievably simple to implement into my current solution.

               

               

               

              Set Variable [ $FieldToSort; Value:Get ( ScriptParameter ) ]

              Set Variable [ $$SortOrder[Code($FieldToSort )]; Value:Case($$SortOrder[Code($FieldToSort)] = "Descending";"Ascending";"Descending") ]
              Go to Object [ Object Name: $FieldToSort ]
              If [ $$SortOrder[Code($FieldToSort)] = "Ascending" ]

                  Sort Records by Field [ Ascending ]

              Else

                  Sort Records by Field [ Descending ]

              End If

              • 4. Re: Sorting multiple fields Up/Down using one script.
                IanRenwick

                Just some further thoughts as it wasn't easy to see what column was the last one sorted.

                 

                So I changed $FieldToSort to $$FieldToSort

                This meant that I could add conditional formatting to the button text so that it changed colour to indicate that it was the "actively" sorted column by adding a calc e.g.  $$FieldToSort = "Company" [ or whatever text matches the button's script parameter ]

                I also added a button with the parameter "Unsort" to unsort the records and clear the conditional formatting based on $$FieldToSort

                 

                Set Variable [ $$FieldToSort; Value:Get ( ScriptParameter ) ]

                If [ $$FieldToSort = "Unsort" ]

                           Unsort Records

                           Exit Script [ ]

                End If


                Set Variable [ $$SortOrder; Value:Case($$SortOrder = "Descending";"Ascending";"Descending") ]
                Go to Object [ Object Name: $$FieldToSort ]
                If [ $$SortOrder = "Ascending" ]

                    Sort Records by Field [ Ascending ]

                Else

                    Sort Records by Field [ Descending ]

                End If