I have a portal which I want to sort by different fields depending on which column header is clicked by the user. I have set up the script to the buttons in the column headers to use this calculated field (call it cSortAsc) :
g_SortDirection = "asc";
g_SortField = "LINE_DOLLARS"; LINE_DOLLARS;
g_SertField = "LINE_CONCEPT"; LINE_CONCEPT;
Then in the portal setup I assign the sort order by cSortAsc and choose ascending. A similar calculated field called cSortDesc is defined similarly except for the matching case g_SortDirection = "desc", and is chosen as second sort field in the portal setup. When the g_SortField is a field other than LINE_DOLLARS and LINE_CONCEPT, then these calculated fields return a constant which effectively does not sort them at all.
The return type of these fields is Text.