Sort the letters of string in alphabetical order: Difference between revisions

Content added Content deleted
(→‎{{header|Lua}}: this seems more like what task had in mind (?))
(→‎{{header|Lua}}: added Lua concise version)
Line 674: Line 674:
{{out}}
{{out}}
<pre>.aaccddeeeeeeffghhhiiiillmmmNnnooooooooorrrstttttttuwy</pre>
<pre>.aaccddeeeeeeffghhhiiiillmmmNnnooooooooorrrstttttttuwy</pre>
Concise version, implicit rather than explicit collation sequence table, adequate for this use, same output:
<lang lua>function sort(s) -- Latin letters lexicographically, uppercase first, anything else by ASCII
local sl,t=string.lower,{} s:gsub("(%S)", function(c) t[#t+1]=c end) -- use "(.)" as pattern to preserve whitespace
table.sort(t, function(a,b) return sl(a)==sl(b) and a<b or sl(a)<sl(b) end) -- implicitly
return table.concat(t)
end

print(sort("Now is the time for all good men to come to the aid of their country."))</lang>


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==