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

m
→‎{{header|Wren}}: Changed to Wren S/H
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Changed to Wren S/H)
(7 intermediate revisions by 6 users not shown)
Line 120:
.Naaccddeeeeeeffghhhiiiillmmmnnooooooooorrrstttttttuwy
</pre>
 
=={{header|ALGOL W}}==
Sorts the letters, leaving the non-letters unsorted.
<syntaxhighlight lang="algolw">
begin
% returns s with the letters sorted %
% as Algol W doesn't have variable length strings, %
% the number of characters to sort must be specified %
% in len %
string(256) procedure lSort( string(256) value s; integer value len ) ;
begin
string(256) letters, sortedS;
integer lLen, lPos, u;
sortedS := s;
% get the letters %
letters := "";
lLen := -1;
for i := 0 until len - 1 do begin
string(1) c;
c := s( i // 1 );
if ( c >= "a" and c <= "z" ) or ( c >= "A" and c <= "Z" ) then begin
lLen := lLen + 1;
letters( lLen // 1 ) := c
end if_we_have_a_letter
end for_i ;
% bubble sort the letters %
u := lLen;
while begin
logical sorted;
sorted := true;
u := u - 1;
for p := 0 until u do begin
string(1) c, d;
c := letters( p // 1 );
d := letters( p + 1 // 1 );
if c > d then begin
letters( p // 1 ) := d;
letters( p + 1 // 1 ) := c;
sorted := false
end if_c_gt_d
end for_p ;
not sorted
end do begin end;
% put the sorted letters into the result string %
lPos := -1;
for i := 0 until len - 1 do begin
string(1) c;
c := s( i // 1 );
if ( c >= "a" and c <= "z" ) or ( c >= "A" and c <= "Z" ) then begin
lpos := lPos + 1;
sortedS( i // 1 ) := letters( lPos // 1 )
end if_we_have_a_letter
end for_i ;
sortedS
end lSort ;
% prints the first len characters of s %
procedure writeOnString( string(256) value s; integer value len ) ;
for i := 0 until len - 1 do writeon( s_w := 0, s( i // 1 ) );
% tests the lSort procedure %
procedure testSort( string(256) value s; integer value len ) ;
begin
writeon( s_w := 0, " [" );writeOnString( s, len ); writeon( "]" );write();
writeon( s_w := 0, " -> [" );writeOnString( lSort( s, len ), len ); writeon( "]" );write()
end testSort ;
 
testSort( "The quick brown fox jumps over the lazy dog, apparently", 55 );
testSort( "Now is the time for all good men to come to the aid of their country.", 69 );
testSort( "Stop! In the name of Wuv!", 25 )
end.
</syntaxhighlight>
{{out}}
<pre>
[The quick brown fox jumps over the lazy dog, apparently]
-> [Taa abcde eeefg hhi jkllm nnoo oop ppqr rrs, ttuuvwxyyz]
[Now is the time for all good men to come to the aid of their country.]
-> [Naa cc dde eeee eff ghh hiii ill mm mnno oo ooo ooo rr rsttt ttttuwy.]
[Stop! In the name of Wuv!]
-> [ISWa! ee fhm nnoo pt tuv!]</pre>
 
=={{header|APL}}==
Line 338 ⟶ 416:
<pre>
" .Naaccddeeeeeeffghhhiiiillmmmnnooooooooorrrstttttttuwy"
</pre>
 
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
Uses Delphi TStringList component to do most of the heavy lifting. The TStringList is designed to hold and maniputlate strings. In the strings are one character long so it sorts characters from the strings.
 
<syntaxhighlight lang="Delphi">
function SortStringLetters(S: string): string;
{Extract letters from string and sort them}
{Uses string list component to hold and sort chars}
var SL: TStringList;
var I: integer;
begin
SL:=TStringList.Create;
try
for I:=1 to Length(S) do SL.Add(S[I]);
SL.Sort;
SetLength(Result,SL.Count);
for I:=0 to SL.Count-1 do
Result[I+1]:=SL[I][1];
finally SL.Free; end;
end;
 
const S1 = 'Now is the time for all good men to come to the aid of the party.';
const S2 = 'See the quick brown fox jump over the lazy dog.';
 
procedure ShowSortedLetters(Memo: TMemo);
var S: string;
begin
Memo.Lines.Add('Unsorted = '+S1);
Memo.Lines.Add('Sorted = '+SortStringLetters(S1));
Memo.Lines.Add('Unsorted = '+S2);
Memo.Lines.Add('Sorted = '+SortStringLetters(S2));
end;
 
</syntaxhighlight>
{{out}}
<pre>
Unsorted = Now is the time for all good men to come to the aid of the party.
Sorted = .aaacddeeeeeeffghhhiiillmmmNnooooooooprrstttttttwy
Unsorted = See the quick brown fox jump over the lazy dog.
Sorted = .abcdeeeeefghhijklmnoooopqrrSttuuvwxyz
 
Elapsed Time: 5.996 ms.
 
</pre>
 
Line 515 ⟶ 640:
print sorted</syntaxhighlight>
{{out}}<pre>aaaaaaaaaabbccddddddddeeeeeeeeefgggghiiIiiiiklmmmnnnnnnnnnOooooopprrrrrrrstttuuuvwwyyyy</pre>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn AlphabetizeString( string as CFStringRef, deleteSpaces as BOOL ) as CFStringRef
NSUInteger i, count
CFStringRef tempStr, alphaStr = NULL
CFArrayRef array
if deleteSpaces then tempStr = fn StringByReplacingOccurrencesOfString( string, @" ", @"" ) else tempStr = string
count = fn StringLength( tempStr )
CFMutableArrayRef mutArr = fn MutableArrayWithCapacity(count)
for i = 0 to count -1
CFStringRef chr = fn StringWithFormat( @"%C", fn StringCharacterAtIndex( tempStr, i ) )
MutableArrayInsertObjectAtIndex( mutArr, chr, i )
next
array = fn ArraySortedArrayUsingSelector( mutArr, @"localizedCaseInsensitiveCompare:" )
alphaStr = fn ArrayComponentsJoinedByString( array, @"" )
end fn = alphaStr
 
CFStringRef testStr
 
testStr = @"The quick brown fox jumped over the lazy dog's back."
print testStr
print "String alphabetized with spaces included:"
print fn AlphabetizeString( testStr, NO )
print
testStr = @"Now is the time for all good men to come to the aid of their country."
print testStr
print "String alphabetized with spaces deleted:"
print fn AlphabetizeString( testStr, YES )
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
The quick brown fox jumped over the lazy dog's back.
String alphabetized with spaces included:
.'aabbccddeeeefghhijkklmnoooopqrrsTtuuvwxyz
 
Now is the time for all good men to come to the aid of their country.
String alphabetized with spaces deleted:
.aaccddeeeeeeffghhhiiiillmmmNnnooooooooorrrstttttttuwy
</pre>
 
 
=={{header|Go}}==
Line 765 ⟶ 936:
</pre>
 
=={{header|K}}==
{{works with|ngn/k}}<syntaxhighlight lang=K>{x@<_x}"This is a test"
" aehiisssTtt"
{x@<_x}"The sentence \"The quick brown fox jumps over the lazy dog\" uses every letter of the alphabet."
" \"\".aaabbccdeeeeeeeeeeeeeeffghhhhhijklllmnnnoooooppqrrrrssssTtTtttttuuuvvwxyyz"
{x@<x}"This is a test" / remove the _ to sort in ascii order rather than alphabetic order
" Taehiissstt"</syntaxhighlight>
=={{header|Lua}}==
<syntaxhighlight lang="lua">fcoll = {} -- forward collation
Line 1,058 ⟶ 1,236:
{{Out}}
<pre>" ?Iaaaaaaaabbcceeefghhhiiiiiijkllllllmnoopppsssssttt"</pre>
 
=={{header|Quackery}}==
 
With regard to the [[Talk:Sort the letters of string in alphabetical order|discussion]] regarding the nature of the task, I am construing the phrases "alphabetical order" and "lexicographical order" to mean QACSFOT order. ('''''Q'''uackery '''A'''rbitrary '''C'''haracter '''S'''equence '''F'''or '''O'''rdered '''T'''ext'')
 
We take the 96 printable characters that Quackery recognises in their native (ASCII/Unicode) order, and sort them into QACSFOT order using a word <code>qacsort</code> that we have defined to do that.
 
<syntaxhighlight lang="Quackery"> [ sortwith [ qacsfot dip qacsfot > ] ] is qacsort ( $ --> $ )
 
[] 94 times [ i^ 1+ space + join ]
 
say "Native order:" cr
dup echo$ cr cr
say "QACSFOT order:" cr
qacsort echo$</syntaxhighlight>
 
{{out}}
 
<pre>Native order:
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
 
QACSFOT order:
0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz()[]{}<>~=+-*/^\|_.,:;?!'"`%@&#$</pre>
 
=={{header|Raku}}==
Line 1,189 ⟶ 1,390:
Output: aaaeeefgggggiilmmnnnooprrrrruv
done...
</pre>
 
=={{header|RPL}}==
{{works with|HP|48G}}
≪ { }
1 3 PICK SIZE '''FOR''' j
OVER j DUP SUB + '''NEXT'''
SWAP DROP
SORT ∑LIST
≫ '<span style="color:blue">STRORT</span>' STO
 
"The quick brown fox jumps over the lazy dog, apparently" <span style="color:blue">STRORT</span>
{{out}}
<pre>
1: " ,Taaabcdeeeefghhijkllmnnoooopppqrrrsttuuvwxyyz"
</pre>
 
Line 1,222 ⟶ 1,438:
,Taaabcdeeeefghhijkllmnnoooopppqrrrsttuuvwxyyz
</pre>
 
=={{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.
<syntaxhighlight lang="ecmascriptwren">var bubbleSort = Fn.new { |s, trim| // allow optional removal of whitespace
var chars = s.toList
var n = chars.count
9,485

edits