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:44, 16 August 2022
, 1 year agoadd another version
(→{{header|Lua}}: added Lua concise version) |
m (add another version) |
||
Line 656:
Unsorted -> Now is the time for all good men to come to the aid of their country.
Sorted -> .aaccddeeeeeeffghhhiiiillmmmNnnooooooooorrrstttttttuwy
</pre>
=== alphabet-only counting version ===
<lang julia>
const alphabets = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
function lettercountingsort(s)
sorted = Char[]
for l in alphabets
append!(sorted, fill(l, count(==(l), s)))
end
return String(sorted)
end
println(lettercountingsort("Now is the time for all good men to come to the aid of their country."))
</lang>
<pre>
aaccddeeeeeeffghhhiiiillmmmNnnooooooooorrrstttttttuwy
</pre>
|