The sort records script step and dialog only sort the records in the layout's found set, not those shown in a portal.
To dynamically sort records in a portal, you have to use one of two methods:
a) set up multiple copies of your portal in different panels of a tab control or slide control. Specify a different sort order for each copy. If this is a tab control, you might just use your tab labels in place of a button, but both the panels of side and tab controls can also be brought to the front if you give them object names and use go to object in your button's script to select a specific panel.
b) it's a trickier option, but you can set up a portal to sort on an unstored calculation that references the value of global fields or global variables to feed into a large case function that copies the value of different fields. So your button updates the value of these fields or variables and since this changes the value of this "sort field" calculation, the records sort into a different order. This is not a simple calculation as you might need to specify a text result type and set up calculations that produce text values for numbers and dates that sort correctly. (You have to pull in leading zeroes...). This is also a sort on an unstored calculation so it can be a bit slow.
a) works best if there is just two or three different sort orders. b) works better when the total number of records in the portal are kept small and is best used for sorting on a single field's value. (Two or N field sorts could become a real monster here...)
I have the same issue
I have a list of related records and want the user to change sort has he need.
Putting button, or using the header names has button seems the best way.
Run into the same problem
In the exemple files "Research notes.fmp12" it would be interesting to sort documents by title, author or year
suggestion a) from philmfdjunk is interesting has long has you don't make any change to the portal
a sort portal would be a great function
hope filemaker team will consider it in futur version
Storing your known sorts in tabs or slide panels like Phil suggested is faster. In 13 and 14 you can also just hide the portals you do not want to see.
I know you can filter a portal with ExecuteSQL and a self join, but I am not sure if you can sort records while filtering if you use ORDER BY in the query. Interesting idea though.
You can use the ORDERed list of result to sort like as
PositionInList ( key_list ; portalTable::key )
PositionInList is some custom function, if the key is not partially match others (like UUID), it can be Position() function.
I you don't have hundreds of lines in your portal then an easy way:
1 Have a "portal sort order" field in your related table
2 use the "replace field contents" script step to change the portal sort order field
3 Use the "refresh portal" script step after that.
If you don't find an easy way to do step 2 then you can do this in your script:
go to related records(portal relationship), show only related records, using layout (portal table layout)
sort (your sort order)
replace field contents::sort order, get(recordnumber)
go to layout(original layout)
It's pretty instant up to a few hundred lines in a portal.
Alternatively if it's a really important portal to your layout consider making the portal rows the "body" of your layout and view it in list view then you have many more options at your hands..