3 Replies Latest reply on May 14, 2017 8:50 AM by TomHays

    why can't i sort by a variable

    PeterCortiel

      I replaced the columns header in a list view with button

      the action passes the name of the column(field)  into a variable called $ fldname

      and I would like it if I dod not have to hardcopy the fieldname in the sort

       

      My script

      Gets the parameter (filename)

      Then decided if desc or ascending by a $$ field

      Sort records by field ( direction ; $fldname)

       

      What am I missing?

       

       

      Peter

        • 1. Re: why can't i sort by a variable
          TomHays

          The usual way using older FileMaker versions is

           

          If [$direction = "ascending"]

             # Sort records by field in ascending order

             Sort Records [Restore]

          Else

             # Sort records by field in descending order

             Sort Records [Restore]

          End If

           

           

          FileMaker 12 or later you can use Sort Record by Field but you still need to select Ascending or Descending (or Associated Value List) hard coded into the script step.

           

          If [$direction = "ascending"]

             # Sort records by field in ascending order

             Sort Records by Field [Ascending; MyTable::MyField]

          Else

             # Sort records by field in descending order

             Sort Records by Field [Descending; MyTable::MyField]

          End If

           

          As you have experienced, the field name must be hard coded.

           

          -Tom

          • 2. Re: why can't i sort by a variable
            PeterCortiel

            Ok here is my form

            2017-05-14_11-37-25.png

             

            Here is my script which would be much easier if I could avoid the hardcoding of the fieldname as I drought now

            I have about 20 list forms with many columns and I could handle that with just one simple script if instead of i.e. Os::osdate i could use $fldname:

            2017-05-14_11-38-14.png

            • 3. Re: why can't i sort by a variable
              TomHays

              If you want to be Extra Special Tricky With Workaround Dohicky, you can do something like

               

              Sort Records by Field [Ascending; MyTable::MyCalcField]

               

              and then put all of the magic into a Calculated field MyCalcField.

              Its calculation could be dependent on $$direction and $$sortByField and use lots of conditionals with hard-coded fields or GetField().

               

              You won't be able to take advantage of the speed of the the hard-coded Sort when attempting to sort by a Stored field since the calculation would have to be Unstored, but you may gain the flexibility you desire.

               

              -Tom

              1 of 1 people found this helpful