Jump to content

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

→‎{{header|Lua}}: added Lua concise version
(→‎{{header|Lua}}: this seems more like what task had in mind (?))
(→‎{{header|Lua}}: added Lua concise version)
Line 674:
{{out}}
<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}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.