| Bytes | Lang | Time | Link |
|---|---|---|---|
| 083 | AWK | 250729T184125Z | xrs |
| 008 | Dyalog APL | 250710T192050Z | Aaron |
| 032 | C clang | 250710T131125Z | jdt |
| 065 | Nim | 250710T005940Z | janAkali |
| 067 | JavaScript Node.js | 250709T125021Z | Fhuvi |
| 087 | Java JDK | 230503T124113Z | Fhuvi |
| 075 | Swift 6 | 250704T174040Z | macOSist |
| 004 | Vyxal 3 | 250704T095310Z | Themooni |
| 095 | Uiua | 240728T041758Z | ErikDaPa |
| 002 | Japt | 230503T110316Z | Shaggy |
| 009 | J | 230503T034219Z | south |
| 020 | Arturo | 230503T014117Z | chunes |
| 034 | Julia 1.0 | 200303T185156Z | wilkben |
| 036 | Powershell | 181211T085254Z | mazzy |
| 002 | 05AB1E | 181211T094230Z | Kevin Cr |
| 002 | MathGolf | 181211T103243Z | maxb |
| 072 | Java 10 | 181211T101159Z | Kevin Cr |
| 050 | PHP | 170427T035952Z | Titus |
| 003 | Jelly | 170427T034332Z | hyperneu |
| 056 | F# | 140122T101814Z | Rik |
| 121 | C | 140122T173531Z | user1220 |
| 083 | C# | 140122T134100Z | Abbas |
| nan | 140203T200450Z | Aaron Da | |
| 009 | k | 140122T122727Z | skeevey |
| 074 | Javascript | 140122T121619Z | eithed |
| 025 | GNU core utils | 140122T093758Z | joeytwid |
| 016 | APL | 140125T172044Z | Mark Plo |
| 039 | PowerShell | 140122T081822Z | Ralf de |
| 033 | Ruby 33 Chars | 140122T123053Z | Siva |
| 012 | J | 140122T113936Z | Gareth |
| 088 | Haskell | 140122T084727Z | vek |
| 048 | R | 140122T110246Z | plannapu |
| 034 | Perl | 140122T084540Z | Dom Hast |
| 026 | Perl6 | 140122T093635Z | Ayiko |
| 246 | GolfScript | 140122T085904Z | Howard |
| 045 | Python 3 | 140122T074959Z | evuez |
AWK, 83 bytes
{for(;i++<26;)for(j=0;j++<NF;)printf sprintf("%c",i+64)~toupper($j)&&$j!~/\./?$j:X}
Dyalog APL, 8 chars
⊂∘⍋∘⎕C⌷⊢
I like that I stumbled upon this solution by accident. I was trying to save a mask of where uppercase letters existed before lowercasing them, made a typo, and then got the solution revealing that I didn't need to convert and unconvert, but can just sort by the lowercased version.
Explanation:
⊂∘⍋∘⎕C⌷⊢
⌷ Select
⊢ from the right argument (the original input)
∘ ∘ the indices returned by the function composed of
⎕C lowercasing...
⍋ sorting...
⊂ and enclosing (because that's how ⌷ likes it)
Like so:
f←⊂∘⍋∘⎕C⌷⊢
f 'Johnny walked the dog to the park.'
.aaddeeeghhhJkklnnoooprtttwy
C (clang), 32 bytes
f(b,n){qsort(b,n,4,strcasecmp);}
Sorts a wide-char string b case-insensitively using qsort()
Treats each wchar_t as a 4-byte char*
Relies on little-endian layout so ASCII characters sit in the lowest byte.
strcasecmp compares only the first byte, ignoring case.
Nim, 65 bytes
include tables,cgi
echo join stdin.readall.sortedByIt toLower $it
Explanation
- we include modules that import
algorithmandstrutilsinternally - command syntax and chaining, thanks to UFCS
- sortedByIt is a template that injects the
itvariable $is a toString operator
JavaScript (Node.js), 67 bytes
As usual, Node's Buffer helps a lot to shorten ASCII conversions
s=>Buffer(s).filter(c=>(d=c|32)>96&d<123).sort((a,b)=>a%32-b%32)+""
In filter , c|32 is used to get the lowercase ASCII codes. Then we check if these codes are in the bounds of a-z's ASCII codes to keep only the letters.
In sort, the double %32 allows to sort ignoring the case.
The +"" at the end, allows to convert the Buffer of ASCII values back into a string.
But still, the shortest JS answer would be a modernized version of eithed 's answer from 11 years ago. Go upvote it!
JavaScript (Node.js), 60 bytes
s=>s.match(/[a-z]/gi).sort((a,b)=>a.localeCompare(b)).join``
Java (JDK), 87 bytes
Not the best Java answer, but i tried to use only inline Stream.
Since the rules aren't clear regarding special characters, here are multiple solutions:
s->s.chars().boxed().sorted((a,b)->a%32-b%32).map(e->""+(char)+e).reduce("",(a,b)->a+b)
returns the correct order for letters, but with special characters mixed in between:
aaddeeeghhhJkklnn.oooprtttwy
Another solution (98 bytes)
s->s.chars().mapToObj(e->""+(char)e).sorted((a,b)->a.compareToIgnoreCase(b)).reduce("",(a,b)->a+b)
returns the correct order for letters, with special characters separated from the letters:
.aaddeeeghhhJkklnnoooprtttwy
And a third solution (119 bytes)
s->s.chars().filter(e->e>64&e<91|e>96&e<123).boxed().sorted((a,b)->a%32-b%32).map(e->""+(char)+e).reduce("",(a,b)->a+b)
returns only the letters, in correct order:
aaddeeeghhhJkklnnoooprtttwy
Swift 6, 75 bytes
{String(($0+"").filter(\.isCased).sorted{$0.lowercased()<$1.lowercased()})}
Vyxal 3, 4 bytes
⑴ʀ↯“
⑴ʀ↯“
↯ # sort by
⑴ʀ # lambda: toLowerCase
“ # join
💎
Created with the help of Luminespire.
<script type="vyxal3">
⑴ʀ↯“
</script>
<script>
args=[["Johnny walked the dog to the park."]]
</script>
<script src="https://themoonisacheese.github.io/snippeterpreter/snippet.js" type="module"/>
Uiua, 9 / 5 bytes
⊏⍏¯⊸⌵▽⌵⊸± => deleting symbols
⊏⍏¯⊸⌵ => ignoring symbols
Explanation:
⊏⍏¯⊸⌵▽⌵⊸±
▽⌵⊸± => keeps letters that are alphabetic
⊏⍏¯⊸⌵ => sort, ignoring case
Powershell, 36 bytes
-join($args-split'\W|(.)'-ne''|sort)
Test script:
$f = {
-join($args-split'\W|(.)'-ne''|sort)
}
@(
,("Johnny walked the dog to the park.", "aaddeeeghhhJkklnnoooprtttwy")
) | % {
$s,$expected = $_
$result = &$f $s
"$("$result"-eq"$expected"): $result"
}
Output:
True: aaddeeeghhhJkklnnoooprtttwy
05AB1E, 3 2 bytes
Σl
Ignores non-letter characters, so some are leading and some are trailing.
A version which removes non-letter characters is 3 bytes instead:
áΣl
Explanation:
á # Optional: only leave the letters ([a-zA-Z]) of the (implicit) input-string
Σ # Sort the characters in the string by (sorted by their ASCII unicode value):
l # Their lowercase equivalent (in case of the letters)
# (And output the result implicitly)
MathGolf, 2 bytes
áδ
Example output
.aaddeeeghhhJkklnnoooprtttwy
Removing non-alphabetical characters
To remove all non-alphabetical characters, this solution works:
áδgÆ∞_δ¡
It is the same as the code above, followed by a filtering where each character is first doubled, and then compared its own capitalization. For example, the string "a" is converted to "aa" and then capitalized to "Aa", which is not equal to "aa". The same way, the string "B" is converted to "BB" and capitalized to "Bb", which is not equal to "BB". However, "." is converted to ".." and is unchanged when capitalized, so it will become filtered out.
Explanation
I really need more string handling in MathGolf... Right now there isn't even an operator to convert to lowercase/uppercase. The only thing I could use was the capitalization operator, which works like an uppercase operator for strings of length 1. This solution also sorts non-alphabetical characters, but those could be ignored. The alphabetical characters preserves their case and are output in the correct order.
á sort by comparator
δ capitalize string
Java 10, 72 bytes (as lambda function)
s->{for(int i=64;++i<91;)for(var c:s)if((c&~32)==i)System.out.print(c);}
But since it's an old challenge stating full program:
Java 10, 126 bytes (as full program)
interface M{static void main(String[]a){for(int i=64;++i<91;)for(var c:a[0].toCharArray())if((c&~32)==i)System.out.print(c);}}
Explanation:
interface M{ // Class
static void main(String[]a){ // Mandatory main method
for(int i=64;++i<91;) // Loop over the uppercase alphabet
for(var c:a[0].toCharArray()) // Inner loop over the characters of the input
if((c&~32) // If the current character converted to uppercase,
==i) // equals the current letter of the alphabet
System.out.print(c);}} // Print the character of the input-loop
PHP, 50 bytes
$a=str_split($argn);natcasesort($a);echo join($a);
does not remove non-letters, takes input from STDIN; run with -R.
Jelly, 3 bytes
ŒlÞ
My first Jelly solution on this site! Thanks to @LeakyNun and @ErikTheOutgolfer for teaching me how to use Jelly and @Dennis for making it! :D
Explanation
ŒlÞ
Þ Sort using the function to its left
Œl Converts to lowercase (because it's sort alphabetically, not by codepoint)
Alternatively, ŒuÞ does the exact same thing except converting to uppercase instead.
F# (68 56)
I'm learning F# so I'm sure this could be shorter:
let f s=s|>Seq.sortBy Char.ToLower|>Seq.iter(printf"%c")
Output:
> f "Johnny walked the dog to the park."
.aaddeeeghhhJkklnnoooprtttwy
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.
C#: 83
Console.Write(new string(Console.ReadLine().OrderBy(i=>i+"".ToLower()).ToArray()));
Update: 65
Executable in LinQPad
new string(Console.ReadLine().OrderBy(i=>i+"").ToArray()).Dump();
q/k4 (3? 5? 8?)
if it's sufficient to enter the code and the input directly into the REPL, it's just asc:
q)asc"Johnny walked the dog to the park."
`s#" .Jaaddeeeghhhkklnnoooprtttwy"
the `s# is bit of q notation that indicates that the string is in sorted order (can be binary searched, etc.). if it has to go, that costs two characters, making five:
q)`#asc"Johnny walked the dog to the park."
" .Jaaddeeeghhhkklnnoooprtttwy"
if you want it provided on stdin, it's time to switch to k4 (and we get rid of the `s# for free), and it's an eight-character solution:
x@<x:0:0
Johnny walked the dog to the park.
" .Jaaddeeeghhhkklnnoooprtttwy"
that one, btw, will work as a code file exactly as is (still eight characters, since q is fine with not having the final newline in a code file). normally there would be issues with a welcome banner and with the REPL staying open, but if you pass the input as a herestring, all that goes away:
$ cat asc.k
x@<x:0:0
$ q asc.k<<<'Johnny walked the dog to the park.'
"\n .Jaaddeeeghhhkklnnoooprtttwy"
$
not actually sure where that extra newline in the output is coming from....
k (10 9)
Reads from stdin
x@<_x:0:0
Example
x@<_x:0:0
Johhny walked the dog to the park.
" .aaddeeeghhhhJkklnoooprtttwy"
Javascript - 74
Unfortunately, due to the way JS sorts characters, we cannot use standard sorting function:
prompt().split("").sort(function(a,b){return a.localeCompare(b)}).join("")
Actually this can be shortened to:
prompt().split("").sort((a,b)=>a.localeCompare(b)).join("")
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. ;-)
APL 16
⍞←A[⍋48|⎕av⍳A←⍞]
Johnny walked the dog to the park.
aaddeeeghhhJkklnnoooprtttwy.
PowerShell : 39
$([string[]][char[]](Read-Host)|sort)" #With spaces and symbols
Result
.aaddeeeghhhJkklnnoooprtttwy
C# : 100
Console.Write(new string(input.ToCharArray().OrderBy(a=>char.ToLower(a)).ToArray()).Trim('.',' '));
Result
aaddeeeghhhJkklnnoooprtttwy
Ruby - 33 Chars
$><<gets.chars.sort(&:casecmp)*''
J, 12 characters
(/:32|a.i.])
Ignores any non-alpha characters.
Haskell, 88
import Data.List
import Data.Char
import Data.Ord
main=interact$sortBy$comparing toLower
(38 without imports from standard lib)
R, 48 characters
cat(sort(unlist(strsplit(scan(,""),""))),sep="")
Example usage:
> cat(sort(unlist(strsplit(scan(,""),""))),sep="")
1: Johnny walked the dog to the park.
8:
Read 7 items
.aaddeeeghhhJkklnnoooprtttwy
Perl 34
Now takes input from STDIN.
print sort{lc$a cmp lc$b}<>=~/\w/g
Perl 18
If output including capitals first and symbols included is acceptable:
print sort<>=~/./g
Perl6: 26 characters
Sorts output uppercase first, then lowercase, deletes symbols/whitespace
say [~] sort comb /\w/,get
If whitespace/symbols in output may be ignored too, this is only 21 characters.
say [~] get.comb.sort
This sorts case-insensitively, keeps symbols (26 chars)
say [~] get.comb.sort: &lc
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
Python 3: 45
print(''.join(sorted(input(),key=str.lower)))