Jump to content

Sort the letters of string in alphabetical order: Difference between revisions

→‎{{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
}
returns = chars.join()
return trim ? s.trim() : s
}
 
var strs = [
var s = "forever wren programming language"
["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]
System.print(s)</lang>
]
for (str in strs) {
System.print(["Unsorted->" + str[0], "Sorted ->" + bubbleSort.call(str[0], str[1])].join("\n"))
System.print(s)</lang>
}</lang>
 
{{out}}
<pre>
var s = "Unsorted->forever wren programming language"
Sorted ->aaaeeeefggggilmmnnnooprrrrruvw
 
Unsorted->Now is the time for all good men to come to the aid of their country.
Sorted -> .Naaccddeeeeeeffghhhiiiillmmmnnooooooooorrrstttttttuwy
</pre>
9,490

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.