Sort the letters of string in alphabetical order: Difference between revisions
Content added Content deleted
m (added sorting category.) |
(→{{header|Wren}}: Altered to accommodate the REXX string as a common comparison.) |
||
Line 121: | Line 121: | ||
=={{header|Wren}}== |
=={{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. |
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| |
<lang ecmascript>var bubbleSort = Fn.new { |s, trim| // allow optional removal of whitespace |
||
var chars = s.toList |
var chars = s.toList |
||
var n = chars.count |
var n = chars.count |
||
Line 135: | Line 135: | ||
if (n == 0) break |
if (n == 0) break |
||
} |
} |
||
s = chars.join() |
|||
return trim ? s.trim() : s |
|||
} |
} |
||
var strs = [ |
|||
⚫ | |||
["forever wren programming language", true], |
|||
s = bubbleSort.call(s).trimStart() // get rid of whitespace which will be at the front |
|||
["Now is the time for all good men to come to the aid of their country.", false] |
|||
⚫ | |||
] |
|||
for (str in strs) { |
|||
System.print(["Unsorted->" + str[0], "Sorted ->" + bubbleSort.call(str[0], str[1])].join("\n")) |
|||
⚫ | |||
}</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
⚫ | |||
aaaeeeefggggilmmnnnooprrrrruvw |
Sorted ->aaaeeeefggggilmmnnnooprrrrruvw |
||
Unsorted->Now is the time for all good men to come to the aid of their country. |
|||
Sorted -> .Naaccddeeeeeeffghhhiiiillmmmnnooooooooorrrstttttttuwy |
|||
</pre> |
</pre> |