Anonymous user
Sort the letters of string in alphabetical order: Difference between revisions
Sort the letters of string in alphabetical order (view source)
Revision as of 18:21, 16 August 2022
, 1 year ago→{{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}}==
|