I’m a fan of the new native implementation of the the arraynumericsort(
function. This new version is lightning fast and handles more numeric text variations than ever. It does not, however, handle the thousands separator so that 1,000 is interpreted as 1. I wanted to use this function to do a sort of a text field containing assorted numeric text values. My solution added a couple of additional traps and seems plenty fast in the limited tests I ran.
My scheme basically involved building an array containing each of the target field’s values plus a tab and the current sequence number. I then did an arraynumericsort(
on that array followed by an arrayfilter
to remove the first column containing the original values and adding a new second column containing the new sequence order. Another arraynumericsort)
on this array gave me the desired sort order in the second column.
At this point I had to use a somewhat heavy handed method to actually sort the target field. This required adding a temporary integer field, filling it with the new desired sort order, sorting on this temporary field and then deleting the temporary field. These actions are hidden between the noshow
and showpage/endnoshow
statements.
My final code has traps to ignore the user’s thousands character as well as the user’s currency symbol. There is also a trap to handle a target field name with spaces and such to enclose the name in chevrons. This is the code I came up with:
startdatabasechange "ALLRECORDS", "Numeric Text Sort"
let _startField=info("fieldname")
let _currentField=chevronquoted(_startField)
let _tempOrder=arraynumericsort(replacemultiple(arraybuild(cr(),"",
_currentField+{+tab()+seq()}),info(“thousandsseparator”)+";"+
info(“currencysymbol”),";",";"),cr())
arrayfilter _tempOrder,_tempOrder,cr(),array(import(),2,tab())+tab()+seq()
_tempOrder=arraynumericsort(_tempOrder,cr())
_tempOrder=arraycolumn(_tempOrder, 2, cr(), tab())
noshow
addfield "_temp_"
field "_temp_"
fieldtype "INTEGER"
formulafill array(_tempOrder,seq(),cr())
sortup
deletefield
showpage
endnoshow
Here is the Numeric Text Sort in action on a field containing many numeric tex variations:
There may be a more elegant way to handle this but since I have no access to the native arraynumericsort(
code (not that I would know what to do with it if I had!), this is the best I could figure out and trimmed down as best I can see. Might be handy for someone else down the line.