# [NTG-context] [Dev-luatex] Example: Sorting in LuaTeX

Hans Hagen pragma at wxs.nl
Thu Aug 9 13:19:56 CEST 2007

Jonathan Sauer wrote:
> Hello Hans,
>
>>> Caveat: In TeX, braces can be used to hide the <delimiter>; this is
>>> not possible in the above code. I plan to code a
>>> "string.findOutsideBraces" function to handle braces correctly, but
> I
>>> wonder if this should not be a library routine.
>> the embedded libaries and extensions are kept to the minimum,
>> if only because we want to avoid endless discussions about
>> what could go in there; lua is fast enough anyway; concerning
>> braces .. watch the %b option on expressions, can be handy
>> for nested braces
>
> Right. I am still learning Lua and had known about patterns, but not
> about %b. That indeed looks promising. I will look into it.

simple example ...

\def\sortedsequence#1#2{\directlua 0 {
do
local sep = "\luaescapestring{#1}"
local str = "\luaescapestring{#2}"
local tab = { }
for s in string.gmatch(str,"([^"..sep.."]+)") do
tab[\string ##tab+1] = s
end
table.sort(tab)
tex.sprint(table.concat(tab,sep))
end
}}

\sortedsequence{,}{qq,aa,cc,bb,ee,xx}

-----------------------------------------------------------------