| Bytes | Lang | Time | Link |
|---|---|---|---|
| 049 | AWK | 250829T161749Z | xrs |
| 001 | Thunno 2 | 230721T062414Z | The Thon |
| 002 | Thunno | 230305T090038Z | The Thon |
| 001 | 05AB1E | 200806T101931Z | Kevin Cr |
| 040 | Brainfuck | 151125T153622Z | Dennis |
| 101 | C++ | 151126T155442Z | Sahil Ar |
| 140 | Brainfuck | 150825T205707Z | CoffeeCl |
| 002 | Ostrich | 150820T201230Z | Alex A. |
| 023 | Ruby | 150821T200219Z | clapp |
| 034 | JavaScript | 150819T141310Z | NinjaBea |
| 061 | C with x86 | 150819T084629Z | lynn |
| 179 | pb INVALID | 150821T195254Z | undergro |
| 035 | Clojure | 150821T175210Z | KChaloux |
| 135 | C++ | 150821T161155Z | user4322 |
| 047 | PHP | 150821T012949Z | GeorgeQ |
| 091 | Lua | 150821T005148Z | Trebuche |
| 049 | Bash + coreutils | 150820T191520Z | pawel.bo |
| 005 | Matlab | 150819T135751Z | Luis Men |
| 020 | jq | 150820T081905Z | gilad ho |
| 027 | PowerShell | 150820T085058Z | Nacht |
| 021 | Julia | 150819T150309Z | Glen O |
| 073 | Nim | 150819T120324Z | Sp3000 |
| 040 | Processing | 150819T165707Z | Kevin Wo |
| 079 | Lua | 150819T161613Z | Henrik I |
| 119 | Java 8 | 150819T135947Z | pretzelK |
| 037 | Powershell | 150819T130902Z | Stephan |
| 032 | JavaScript ES6 | 150819T145510Z | rink.att |
| 005 | Stuck | 150819T141208Z | Kade |
| 430 | GOTO++ | 150819T080519Z | Fatalize |
| 110 | C# | 150819T074003Z | user3094 |
| 054 | JavaScript | 150819T115327Z | Marcel |
| 004 | Stuck | 150819T101353Z | lynn |
| 001 | gs2 | 150819T101904Z | lynn |
| 021 | Scala | 150819T103938Z | gilad ho |
| 007 | APL | 150819T103600Z | lynn |
| 046 | Idris | 150819T102605Z | lynn |
| 032 | Python 2 | 150819T092757Z | Blue |
| 017 | Ruby | 150819T082500Z | lynn |
| 023 | Coreutils | 150819T074056Z | Thor |
| 018 | Perl | 150819T074313Z | samgak |
| 002 | Pyth | 150819T083329Z | Kamehame |
| 035 | Haskell | 150819T081246Z | lynn |
| 003 | J | 150819T083421Z | Fatalize |
| 034 | SWIProlog | 150819T081830Z | Fatalize |
| 002 | CJam | 150819T081410Z | lynn |
| 001 | GolfScript | 150819T072955Z | jimmy230 |
| 031 | Python 3 | 150819T074938Z | Kamehame |
AWK, 49 bytes
{l=split($0,a,X);for(asort(a);j++<l;)printf a[j]}
Here's a version without a sort function that I thought was fun:
@load"ordchr"
{l=split($0,a,OFS=$0=X);for(;i++<l;)$t=a[i]$(t=ord(a[i]))}1
05AB1E, 1 byte
{
Try it online or verify a few more test cases.
Also works in 05AB1E (legacy) or 2sable, which are older versions of 05AB1E.
Explanation:
{ # Sort the characters of the (implicit) input-string
# (after which the result is output implicitly)
Brainfuck, 40 bytes
,[>>+>>,]<<[[<<]>>[-[<]>>[.<<->]>+>>]<<]
This uses the counting sort algorithm, which makes this an O(n) solution.
The code requires a left-infinite or wrapping tape of 8 bit cells. Try it online!
How it works
, Read a char from STDIN.
[ While the byte under the pointer (last read char) is non-zero:
>>+ Move the pointer two steps to the right and increment.
>>, Move the pointer two steps to the right and read a char.
]
<< Move the pointer two steps to the left.
If the input was "sort", the tape now contains the following:
0 0 115 0 1 0 111 0 1 0 114 0 1 0 116 0 1 0 0
^
[ While the byte under the pointer is non-zero:
[<<] Advance two steps to the left until a null byte is encountered.
>> Advance two steps to the right.
This will place the pointer on the first input character.
[ While the byte under the pointer is non-zero:
- Decrement.
[<] Move the pointer to the left until a null byte is encountered.
>> Move the pointer two steps to the right.
If the decremented character is non-zero, [<] will move to the
null byte before it, so >> brings the pointer to the null byte
after it. If the decremented character is zero, [<] is a no-op, so
>> advances two steps to the right, to a non-zero byte.
[ While the byte under the pointer is non-zero:
. Print the char under the pointer.
<<- Move the pointer two steps to the left and decrement.
> Move the pointer to the right.
]
If the decremented character gave zero, this will print the value
of the accumulator after it, and decrement the character once more
to make it non-zero, then place the pointer to the right of the
character, thus exiting the loop.
>+ Move the pointer to the right and increment.
This increments the accumulator each time an input character is
decremented.
>> Move the pointer two steps to the right.
This moves the pointer to the next character.
]
<< Move the pointer two steps to the left.
This moves the pointer to the accumulator of the last character.
]
After 255, th accumulator wraps around to 0, and the loop ends.
C++, 101 Bytes
#include<bits/stdc++.h>
using namespace std;main(){string s;cin>>s;sort(s.begin(),s.end());cout<<s;}
Brainfuck, 140 bytes
Probably not the winning post, but still it was fun :)
----------[++++++++++>>,----------]<<[[<<]>>>>[[<<[[-<+>]>>>+<<<]<[->+<]>>>>[-<-<+<->>>>>+<<]>>-[+<<<[-<<+>>]>>>]<<<]<[-<+>>+<]>>>]<<.[-]<<]
Explanation:
Program loads whole line and performs bubble sort. After each sort iteration prints and removes last character. It's always the smallest one, because bubble sort guarantees, that after every iteration the lowest value is in the end. Stops after removal of all characters.
#read line
----------[++++++++++>>,----------]<<
#MEMORY MODEL OF WHOLE PROGRAM:
# 0|0|?|0|?|0|?|0|?|0|0
# C C C C
# C-character from input
#iterate if there are characters
[
#go to first letter
[<<]>>
#MEMORY MODEL OF TWO SORTED CHARACTERS:
# 0|?|0|?|0|?|0|?
# S L M R F G
# S - swap of left during left 0 check
# L - left character
# M - middle, difference between left and right
# R - right character
# F - flag, set if left was not 0
# G - next flag, set if left was 0
#sort two characters if there are at least two
>>[
#while right is not 0
[
#if left is not 0
<<[[-<+>]
#set flag
>>>+<<<
#clean up after left's 0 check
]<[->+<]>
#if flag is set, so left is not 0
>>>[-
#move 1 from left and right to the middle
<-<+<->>>
#set next flag
>>+<<
]
#if next flag is not set, so left was 0
>>-[+
#transfer right to left
<<<[-<<+>>]>>>
]
#while right is not 0
<<<]
#distribute middle to left and right
<[-<+>>+<]>
#end if it was the last character
>>]
#write and clear last character
<<.[-]
#end if no more characters
<<]
Ruby, 23 bytes
puts gets.chars.sort*''
Splits string by empty regexp, sorts array, joins array. Thanks to @MartinBüttner for 6 bytes :D
JavaScript, 34 bytes
alert([...prompt()].sort().join``)
The reason this is so long is that JavaScript can only sort arrays, so the string must be split into an array, sorted, and then joined back into a string. This is ECMAScript 6; the equivalent in ES5 is:
alert(prompt().split('').sort().join(''))
C (with x86), 61 bytes
s[];main(){qsort(s,read(0,s,99),1,"YXZQQQ\x8a\x00*\x02\x0f\xbe\xc0\xc3");puts(s);}
That string contains raw bytes, not actual \x.. codes, and it's a raw machine code callback passed to qsort. Works on x86 only:
59 pop ecx
58 pop eax
5a pop edx
51 push ecx
51 push ecx
51 push ecx
8a 00 mov al, BYTE PTR [eax]
2a 02 sub al, BYTE PTR [edx]
0f be c0 movsx eax, al
c3 ret
Which is essentially:
int func(char *a, char *b) { return *a - *b; }
See p6-7 of this pamphlet in Japanese by shinh.
pb (INVALID), 179 bytes
^w[B!0]{w[B!0]{>}<t[B]^b[T]vb[0]<[X]<vb[B+1]^>}^w[B!0]{w[B!0]{>}<t[B]b[0]<[X]v[T+2]w[B!0]{>}b[T]<[X]^[Y+2]}vv<w[B!0]{>vw[B=0]{v}w[B!0]{>}<t[B]b[0]^[Y]<[X]w[B!0]{>}b[T]<[X]<b[B-1]}
Answers on this site are only considered valid if the language they're written in was available (e.g. there was an interpreter) at the time the question was posted. I didn't finish pb's interpreter until the day after, so this answer is just for fun and is ineligible to win (not that it was going to). That's also why I formatted the header of this answer incorrectly; I don't want to show up in the leaderboard snippet :)
Here's the code with comments, though I'm not sure they'll be helpful to anyone trying to decipher this. I just wrote them to keep my thoughts straight while writing the program, so they're kind of redundant and all over the place.
^w[B!0]{ # while there's something at (0, -1)
w[B!0]{>}<t[B]^b[T]vb[0] # move last byte of input up by 1
<[X]<v # go to (-1, 0)
b[B+1] # increase by 1
^>} # restart loop
^w[B!0]{ # while there's something at (0, -2)
w[B!0]{>}<t[B]b[0] # save last byte of input and erase it
<[X] # go to (0, -2)
v[T+2] # go down T+2
w[B!0]{>}b[T] # go right until there's nothing there. write T
<[X]^[Y+2]} # restart loop
vv<w[B!0]{ # while there's something at (-1, 0)
>vw[B=0]{v} # go down the X=0 column until you find something
w[B!0]{>}<t[B]b[0] # save the rightmost thing and erase it
^[Y]<[X]w[B!0]{>}b[T] # add the thing you got to Y=0, to be outputted
<[X]<b[B-1]} # decrease (-1, 0) by 1 and repeat loop
Clojure, 35 Bytes
(print(apply str(sort(read-line))))
This will prompt the user for input, sort the characters, rejoin them back into a string and print it.
However, we can make it 3 bytes smaller if we allow the result to include quotations:
(pr(apply str(sort(read-line))))
pr is a debugging tool that prints the object it's given rather than its contents, so instead of showing hist in the console, it will show "hist".
C++, 135 bytes
#include <iostream>
#include <string>
#include <algorithm>
main(){std::string s;std::cin>>s;std::sort(s.begin(),s.end());std::cout<<s;}
http://coliru.stacked-crooked.com/a/7c9d5c37c2783a23
I payed for what I used :P
PHP, 47 bytes
$a=str_split($argv[1]);asort($a);echo join($a);
Lua, 91 bytes
a=table t={}for c in arg[1]:gmatch(".") do t[#t+1]=c end a.sort(t)print(a.concat(t))
Lua has no string sort function or split/explode function, so it was a struggle
Bash + coreutils (49 bytes)
Bash was not yet submitted, here is a script (needs to be in a file, as it recurses):
[ $1 ]&&(echo ${1::1};$0 ${1:1})|sort|tr -d '\n'
I just recursively output head, call the script with the tail of the word in order to split the input word ($1) to single chars and then use sort util.
However I believe a shorter solution using bash+coreutils exists... Any ideas?
Matlab, 13 5 bytes
The code simply defines a function handle to the sort function:
@sort
To call it, use ans('this'), where 'this' represents the input string (thanks to Alex A. and Stewie Griffin):
>> @sort
ans =
@sort
>> ans('this')
ans =
hist
PowerShell, 27 bytes
%{([char[]]$_|sort)-join''}
Julia, 21 bytes
s->join(sort([s...]))
And for fun, here's how you might do it without using an inbuilt sorting function, for 53 bytes:
f=s->s>""?(k=indmax(s);f(s[k+1:end]s[1:k-1])s[k:k]):s
Nim, 102 101 79 73 bytes
let s=stdin.readAll
for i in 1..'~':
for j in s:(if i==j:stdout.write j)
Still learning Nim and working out golf tricks. Apparently it's better not to use the builtin sort, which would require a lot of imports (thanks @Mauris)
Processing, 40 bytes
print(join(sort(args[0].split("")),""));
Lua, 79 bytes
t=table for c in({...})[1]:gmatch"."do t:insert(c)end t:sort()print(t:concat())
Obviously, I could save a few bytes if the string was already in a variable (say, s):
t=table for c in s:gmatch"."do t:insert(c)end t:sort()print(t:concat())
Java 8, 119 bytes
This is basically only competitive with the C# answer, because, well, Java.
(At least this beats GOTO++. Not really an accomplishment...)
class C{public static void main(String[]s){s=s[0].split("");java.util.Arrays.sort(s);System.out.print("".join("",s));}}
Thanks to ProgramFOX for saving 1 byte, rink.attendant for saving 2 bytes.
Powershell, 44 37 Bytes
-join((Read-Host).ToCharArray()|sort)
JavaScript (ES6), 32 bytes
Demo only works in Firefox and Edge at time of writing, as Chrome/Opera does not support ES6 by default:
Edit: I didn't look at the answers prior to posting but now I realize it's pretty much the exact same as the one by NinjaBearMonkey.
f=x=>alert([...x].sort().join``)
<form action=# onsubmit='f(document.getElementById("I").value);return false;'>
<input type=text pattern=\w+ id=I>
<button type=submit>Sort letters</button>
</form>
Stuck, 5 Bytes
I finally get to use my language, Stuck! :D
s$""j
This takes an input via stdin, sorts, joins, and implicitly prints. This did give me some ideas for changes though.
Edit: Oh wow, someone already posted and beat me in my own language!
GOTO++, 432 430 bytes
niveaugourou 0
s=ENTRETONTEXTE()
§2 a=LeCaracNumero()&s *(1)
n=*(1)
costaud i=*(2)/&i infeg NombreDeLettres(&s)/i=+*(1)
b=LeCaracNumero()&s &i
GOTONULPOURLESNULS %1 }&b inf &a{
a=&b
n=&i
§1 faiblard
GOTOPRINTDUTEXTE()&a
t=PrendsUnMorceau()&s *(0) &n
u=PrendsUnMorceau()&s }&n+*(1){ *(0)
e=BOITEAPINGOUINS()&t &u
s=Marijuana()&e «»
GOTONONNULPOURLESNULS %3 }NombreDeLettres(&s) eg *(1){
GOTOPASMALIN %2
§3 GOTOPRINTDUTEXTE()&s
Not sure why I inflicted this to myself, but I did
C#, 114 110 characters
Takes input from a command line argument. Not a very short program, but well... it's C#. :P
namespace System.Linq{class P{static void Main(string[]a){Console.Write(string.Concat(a[0].OrderBy(x=>x)));}}}
Thanks to Abbas for saving 4 bytes!
JavaScript, 54 bytes
call js file with node
console.log(process.argv[2].split('').sort().join(''))
Scala, 21 bytes
print(args(0).sorted)
run from command line example:
$ scala -e "print(args(0).sorted)" this
hist
APL, 7 characters
Doesn't work on ngn-apl for me, but should work in theory:
X[⍋X←⍞]
⍞ reads a line from standard input, which is assigned to X. ⍋X is the indices of X which yield an ascending order, and X[...] actually sorts X by these indices.
Idris, 46 bytes
main:IO();main=putStr$pack$sort$unpack!getLine
Python 2, 33 32 bytes
print`sorted(raw_input())`[2::5]
Heavily inspired by @Kamehameha's answer. Converted to python 2. Can't golf much more.
Ruby, 17 bytes
$><<$<.chars.sort
Coreutils, 24 23
fold -w1|sort|tr -d \\n
Perl, 18 bytes
print sort<>=~/./g
Thanks to Dom Hastings for helping me save 3 bytes.
Haskell, 35 bytes
import Data.List;main=interact sort
J, 3 bytes
/:~
For example: /:~'this'
SWI-Prolog, 34 bytes
a(X):-msort(X,Y),writef("%s",[Y]).
Called as such:a(`this`).
CJam, 2 bytes
l$
Reads a line of input (l) and sorts it ($).
Python 3, 31 Bytes
print("".join(sorted(input())))