Sort the letters of string in alphabetical order: Difference between revisions
Sort the letters of string in alphabetical order (view source)
Revision as of 17:48, 24 July 2021
, 2 years ago→{{header|Wren}}: Altered to accommodate the REXX string as a common comparison.
m (added sorting category.) |
(→{{header|Wren}}: Altered to accommodate the REXX string as a common comparison.) |
||
Line 121:
=={{header|Wren}}==
Well, we'll write a function for a bubble sort which we don't have in Wren-sort because it's normally much slower than the other methods. However, it's fast enough here.
<lang ecmascript>var bubbleSort = Fn.new { |s, trim| // allow optional removal of whitespace
var chars = s.toList
var n = chars.count
Line 135:
if (n == 0) break
}
return trim ? s.trim() : s
}
var strs = [
var s = "forever wren programming language"▼
["forever wren programming language", true],
["Now is the time for all good men to come to the aid of their country.", false]
System.print(s)</lang>▼
]
for (str in strs) {
System.print(["Unsorted->" + str[0], "Sorted ->" + bubbleSort.call(str[0], str[1])].join("\n"))
}</lang>
{{out}}
<pre>
Sorted ->aaaeeeefggggilmmnnnooprrrrruvw
Unsorted->Now is the time for all good men to come to the aid of their country.
Sorted -> .Naaccddeeeeeeffghhhiiiillmmmnnooooooooorrrstttttttuwy
</pre>
|