Display Sentence in Alphabetical Order

GolfScript, 24 / 6 characters

{26,{65+.32+}%?)},{31&}$

Example:

> Johnny walked the dog to the park.
aaddeeeghhhJkklnnoooprtttwy  

If the input is restricted to printable ascii the code can be shortened by three characters using {95&.64>\91<&}, as filter.

Can be tested here.

The can-be-ignored version is even shorter (6 chars):

{31&}$

and yields output

> Johnny walked the dog to the park.
      aaddeeeghhhJkkl.nnoooprtttwy

GNU core utils - 25 characters (29 dropping symbols)

fold -1|sort -f|tr -d \\n

Example (from GNU bash 3):

$ echo "Johnny walked the dog to the park."|fold -1|sort -f|tr -d \\n
      .aaddeeeghhhJkklnnoooprtttwy   <<no trailing newline>>

From the question:

Spaces and symbols can be ignored or deleted

I chose to leave them in! To retain only alphabetic characters, replace fold -1 with grep -o \\w for +4 characters.

grep -o \\w|sort -f|tr -d \\n

Thanks to Firefly for recommending grep -o over sed, and Wumpus for fold -1. ;-)


C, 121

This is quite long compared to other entries, but it does not rely on any built-in sorting or ToLower functions:

j;main(k){char s[99],*p=s;gets(s);while(*p){j=p-s-1;k=*p++;while(j>=0&&(s[j]|32)>(k|32))s[j+1]=s[j--];s[j+1]=k;}puts(s);}

More readable version:

j; main(k) {
    char s[99], *p=s;
    gets(s);
    while(*p) {
        j = p-s-1;
        k = *p++;
        while(j >= 0 && (s[j]|32) > (k|32))
            s[j+1] = s[j--];
        s[j+1] = k;
    }
    puts(s);
}

This is an implementation of insertion sort with a case-insensitive comparison between elements (using the |32 bitwise operation). This is because in ASCII encoding uppercase letters and lowercase letters only differ by the 25 bit.

Tags:

Code Golf