Jump to content

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

Added Algol 68
(→‎{{header|Wren}}: Altered to accommodate the REXX string as a common comparison.)
(Added Algol 68)
Line 14:
Write the function even if your language has a built-in function for it.
<br><br>
 
=={{header|ALGOL 68}}==
As with the Wren, Go and probably other samples, this defines a bubble sort to sort the text. Non-alphabetic characters are retained.
<lang algol68>BEGIN
# returns s with the characters sorted into lexicographic order #
OP LSORT = ( STRING s )STRING:
BEGIN
[ 1 : UPB s[ @ 1 ] ]CHAR c := s[ @ 1 ];
FOR u FROM UPB c - 1 BY -1 TO LWB c
WHILE
BOOL sorted := TRUE;
FOR p FROM LWB c BY 1 TO u DO
IF c[ p ] > c[ p + 1 ] THEN
CHAR t := c[ p ];
c[ p ] := c[ p + 1 ];
c[ p + 1 ] := t;
sorted := FALSE
FI
OD;
NOT sorted
DO SKIP OD;
c
END; # SORT #
print( ( LSORT "The quick brown fox junps over the lay dog, apparently", newline ) )
END</lang>
{{out}}
<pre>
,Taaabcdeeeefghhijkllnnnoooopppqrrrsttuuvwxyy
</pre>
 
=={{header|Go}}==
3,060

edits

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