What is a Word Signature?

The signature of a word are all of it's letters put in order - the signatures of this, hist and hits are all hist.

The Challenge

Write a program program should ask for an input, then print out the signature of that word. So an example program run might look like this:

Give me a string: this
The signature signature of 'this' is hist.

There is no required interaction, as long as it takes an input and prints the signature, it is OK.


The program with the smallest byte count wins!


Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

# Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

# Ruby, <s>104</s> <s>101</s> 96 bytes

var QUESTION_ID=54945;function answersUrl(e){return"http://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),e.has_more?getAnswers():process()}})}function shouldHaveHeading(e){var a=!1,r=e.body_markdown.split("\n");try{a|=/^#/.test(e.body_markdown),a|=["-","="].indexOf(r[1][0])>-1,a&=LANGUAGE_REG.test(e.body_markdown)}catch(n){}return a}function shouldHaveScore(e){var a=!1;try{a|=SIZE_REG.test(e.body_markdown.split("\n")[0])}catch(r){}return a}function getAuthorName(e){return e.owner.display_name}function process(){answers=answers.filter(shouldHaveScore).filter(shouldHaveHeading),answers.sort(function(e,a){var r=+(e.body_markdown.split("\n")[0].match(SIZE_REG)||[1/0])[0],n=+(a.body_markdown.split("\n")[0].match(SIZE_REG)||[1/0])[0];return r-n});var e={},a=1,r=null,n=1;answers.forEach(function(s){var t=s.body_markdown.split("\n")[0],o=jQuery("#answer-template").html(),l=(t.match(NUMBER_REG)[0],(t.match(SIZE_REG)||[0])[0]),c=t.match(LANGUAGE_REG)[1],i=getAuthorName(s);l!=r&&(n=a),r=l,++a,o=o.replace("{{PLACE}}",n+".").replace("{{NAME}}",i).replace("{{LANGUAGE}}",c).replace("{{SIZE}}",l).replace("{{LINK}}",s.share_link),o=jQuery(o),jQuery("#answers").append(o),e[c]=e[c]||{lang:c,user:i,size:l,link:s.share_link}});var s=[];for(var t in e)e.hasOwnProperty(t)&&s.push(e[t]);s.sort(function(e,a){return e.lang>a.lang?1:e.lang<a.lang?-1:0});for(var o=0;o<s.length;++o){var l=jQuery("#language-template").html(),t=s[o];l=l.replace("{{LANGUAGE}}",t.lang).replace("{{NAME}}",t.user).replace("{{SIZE}}",t.size).replace("{{LINK}}",t.link),l=jQuery(l),jQuery("#languages").append(l)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",answers=[],page=1;getAnswers();var SIZE_REG=/\d+(?=[^\d&]*(?:&lt;(?:s&gt;[^&]*&lt;\/s&gt;|[^&]+&gt;)[^\d&]*)*$)/,NUMBER_REG=/\d+/,LANGUAGE_REG=/^#*\s*([^,]+)/;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"><div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table></div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table></div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody></table><table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody></table>

Challenged Finished!

WINNER: jimmy23013

  • 4
    \$\begingroup\$ We don't have to print the sentences Give me a string: and The signature signature of 'this' is right? We can just take the input directly and print the result? \$\endgroup\$
    – Fatalize
    Commented Aug 19, 2015 at 7:24
  • 5
    \$\begingroup\$ What about duplicate letters? e.g. is the signature of broom bmoor or bmor? \$\endgroup\$
    – samgak
    Commented Aug 19, 2015 at 7:39
  • 4
    \$\begingroup\$ @isaacg why are you? \$\endgroup\$
    – Tim
    Commented Aug 19, 2015 at 10:52
  • 3
    \$\begingroup\$ @Tim I don't think we should have challenges that are this straightforward. I'll ask about it on meta - it's not really about this question in particular. \$\endgroup\$
    – isaacg
    Commented Aug 19, 2015 at 11:00
  • 2
    \$\begingroup\$ I think this would have been a more interesting challenge if you couldn't use built-in sorting functions. \$\endgroup\$
    – Glen O
    Commented Aug 20, 2015 at 4:34

46 Answers 46


jq, 20 bytes


try it here


Matlab, 13 5 bytes

The code simply defines a function handle to the sort function:


To call it, use ans('this'), where 'this' represents the input string (thanks to Alex A. and Stewie Griffin):

>> @sort
ans = 
>> ans('this')
ans =
  • \$\begingroup\$ Gotta love f('this'). I don't think you need to count the name of the function in your byte count though, since unnamed functions are generally acceptable. \$\endgroup\$
    – Alex A.
    Commented Aug 19, 2015 at 20:16
  • \$\begingroup\$ @AlexA Thanks! But if I define the function without the function handle f , i.e. just @(x)sort(x), then the call can't (obviously) be f('this'). It needs to be the uglier and cumbersome feval(@(x)sort(x),'this'). Do you think that would be acceptable? \$\endgroup\$
    – Luis Mendo
    Commented Aug 20, 2015 at 0:22
  • \$\begingroup\$ Yep, that's very common. Often folks just say, "to call the function give it a name." \$\endgroup\$
    – Alex A.
    Commented Aug 20, 2015 at 2:58
  • \$\begingroup\$ I don't like this at all, but you can actually do: @(x)sort(x), and then ans('this'). I guess you don't need to count the bytes for ans..? Obviously this only works one time, but nothing in the rules prevent this. Also, I would claim sort('this') should suffice, seeing that everyone else just uses the built in sort functions without giving it a different name first. \$\endgroup\$ Commented Aug 20, 2015 at 12:36
  • \$\begingroup\$ @StewieGriffin Hey, good idea to use ans! I agree it's ugly, but the very purpose of code golf is to abuse language. As for your second proposal, I wouldn't know how many bytes to count then :-) \$\endgroup\$
    – Luis Mendo
    Commented Aug 20, 2015 at 15:13

PHP, 47 bytes

$a=str_split($argv[1]);asort($a);echo join($a);

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;}


I payed for what I used :P

  • \$\begingroup\$ The downvote wasn't mine, but it may have something to do with this not being valid C++ because main() must have an int return \$\endgroup\$
    – Tas
    Commented Aug 24, 2015 at 3:37
  • \$\begingroup\$ @Tas The question doesn't specify that it must be strictly valid code. If it works, it works. \$\endgroup\$ Commented Sep 9, 2015 at 3:33
  • 1
    \$\begingroup\$ payed paid... \$\endgroup\$ Commented Sep 9, 2015 at 3:36

pb (INVALID), 179 bytes


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

Ruby, 23 bytes

puts gets.chars.sort*''

Splits string by empty regexp, sorts array, joins array. Thanks to @MartinBüttner for 6 bytes :D


Brainfuck, 140 bytes

Probably not the winning post, but still it was fun :)



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

# 0|0|?|0|?|0|?|0|?|0|0
#     C   C   C   C
# C-character from input

#iterate if there are characters
    #go to first letter

    # 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

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.


{  # Sort the characters of the (implicit) input-string
   # (after which the result is output implicitly)

Idris, 46 bytes


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())

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?


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


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++, 101 Bytes

using namespace std;main(){string s;cin>>s;sort(s.begin(),s.end());cout<<s;}

Thunno, 2 bytes


Attempt This Online!

Output as a list of characters. Add the J flag if you want a string.


Thunno 2, 1 byte

Try it online!

Built-in for "sort".


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.