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:41, 15 August 2022
, 1 year ago→{{header|Lua}}: this seems more like what task had in mind (?)
(→{{header|Lua}}: added Lua solution) |
(→{{header|Lua}}: this seems more like what task had in mind (?)) |
||
Line 659:
=={{header|Lua}}==
<lang lua>fcoll = {} -- forward collation
sl = string.lower -- for case insensitivity
for i=0,255 do fcoll[i]=string.char(i) end -- initially just ASCII (for non-letters)
rcoll = {} for i,v in ipairs(fcoll) do rcoll[v]=i end -- reverse collation
function sort(s) -- Latin letters lexicographically, uppercase first, anything else by ASCII
local t={} s:gsub("(%S)", function(c) t[#t+1]=c end) -- use "(.)" as pattern to preserve whitespace
table.sort(t, function(a,b) return rcoll[a]<rcoll[b] end)
return table.concat(t)
end
print(
{{out}}
<pre>.aaccddeeeeeeffghhhiiiillmmmNnnooooooooorrrstttttttuwy</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
|