Portal sorting is based on a field, so what you need is a field in your portal table that will calculate the correct sort value for you.
So in essence when your data is:
Your sort key field contains:
To accomplish this in your use case, I would use a script that replaces the related records sort key with a value, and then have that field contain an auto-enter calc that enters the next highest value (EG if the highest sort order is 3, enter 4).
1) Create the field in your portal table: portalTable::sortKey - type numeric. Set it with an auto-enter calculation of:
ExecuteSQL("SELECT MAX(sortKey) FROM portalTable";"";"") + 1
This will auto-enter the next highest number in order. Doesn't matter if you have sort orders specified for other records as well.
2) Set your portal to sort records by portalTable::sortKey ascending.
3) Create a script that refreshes the sort values on demand:
Go To Related Records [ from portalTable, matching current record, using any portalTable layout]
Sort Records [ portalTable::ID ascending ]
Replace Field Contents [ portalTable::sortKey ; serial numbers starting with 1, incrementing by 1]
Go To Layout [ your main screen ]
Refresh Window [ flush cached content ]
That's very helpful. Thanks a lot. I kind of know this way but yours is way better than what i thought in my mind. Anyway, i would like to find a way to do this by not specify a field in the portal. just simply by sort by field script for example. (Maybe it does not work.)
Please someone confirm that for me, so i just have to use this way then.
The reason i was a bit "picky" here is i am changing this on an existing file.(ideally i want to do very minimum changes and make sure very minimum impact on exist system)
Portals are sorted either by the relationship from the parent to the portal table, or by the checkbox under the portal settings dialog.
Thus a field IS required to sort a portal. What field you specify is up to you, and you can certainly customize the field contents like I have shown you.
However, there is no "sort portal" script step that will magically override the portal settings you have established, those are coded to the layout.
There is a more complex option that allows you to specify the sort order with a value list, but it's much more difficult to explain and not any better to use in your case.
IMHO the best answer to your question needs to know how many records are behind that portal (and how many you think you'll have in a year from now). How many users also counts.
For example, one "think different" idea could be to have 2 relationships, a sorted one and a normal one. 2 portals, one over the other, and a "hide when" which decides which one is being displayed.
the method I described handles multiple users as well. There is the potential that it could slow down under a ton of related records in the portal, but at that point you could switch to "perform script on server" to speed it back up again.
Replace field contents is not so multi user friendly in my book.