"local theList=...; local t=luafy.totable(theList);table.sort(t);return luafy.tolist(t)";
Benchmarking with FileMaker Pro Advanced 11 on an Intel Macintosh shows that sorting with LuaFy in this way is about 250 times faster than using the custom function SortList() when sorting 1000 items (1 sec. versus 0.004 sec.). Lua is over 1000 times faster when sorting 10,000 items. Sorting 100,000 entries in a FileMaker value list is no problem for Lua (~ 0.5 sec on a 2009 Quad-core Mac Pro).
Note that when comparing the results of Lua's sorting versus FileMaker sorting, remember that
Lua's default sort order is case-sensitive and in ASCII order. You can make Lua's table.sort()
be case-insensitive by providing the optional second argument that makes the comparision convert
both arguments to lower case before comparing them, e.g.
table.sort(myArray, function(t1,t2) return (t1:lower() < t2:lower() ) end )
LuaFy_CallFunction("string.match";"We have 42 products in stock."; "%d+ %a+")
for theMatch in string.gmatch(t , '%d+ %a+') do
r = r .. theMatch .. '\r'
"We have 3 blue, 2 green, and 5 red in stock.")
Example: LuaFy_CallFunction("string.gsub"; "This is an example sentence."; "(exam%a+)"; "excellent")
Result: This is an excellent sentence.
Say that you have a records with a identifier that has a mixture of letters and numeric digits. You want to sort based on the identifier but FileMaker Pro's alphabetic sorting does not recognize the numeric sections of your identifier in the way you would like. If you could generate a sort key that padded all of the numbers with zeros so that they would sort alphabetically, then you could simply sort using that new sort key. If your digits appear in a variety of places in the string, this could present a significant challenge for a FileMaker Pro calculation.
These identifiers are in alphabetical order but not the way a human may prefer to see them.
If you prefer to see them in this order...
Then the sort key needs to be something like the following.
|Record #||Identifier||Desired Sort Key|
Lua's string.gsub() can handle this if we use a function instead of a simple pattern as the replacement. If your identifier is in the field named "SKU", you can create a calculated field "SortKey" which can be defined with this short calculation:
local sortKey = ...
if (sortKey == nil) or (sortKey == '') then return '' end
sortKey = sortKey:gsub('(%d+)', function(n) return string.format('%08d',n) end)
Any sequence of digits in the input will be expanded to 8 digits by padding on the left with zeros.