Golf the Pigeonhole sort Implement the Pigeonhole sort. This is a variant of the Bucket sort, and its logic is very simple: index your items (by a given function) and then return all non-empty indexes' items.

Your program/function should get an array of items in the most comfortable way for your language of choice, (i.e. a parameter, stdin, etc.), and in any format you like (for example: JSON). It should return/output a sorted array.

Do not modify the given array.

The second parameter that will be given to your program/function will be the "sort-by function". For exmaple, in JavaScript it will be something like function(item){return item.id;}.

For simplicity, you can assume this function returns a unique positive Integer for each item in the given array.

Shortest code wins. Only ASCII is allowed.
Ruby, 43

def s a,*b,&amp;f
a.map{|x|b[f[x]]=x}
b-[p]
end


Calling looks like

p s(["a","ccc","bb","eeeee"],&amp;:length)
  -&gt; ["a", "bb", "ccc", "eeeee"]


Tricks: *b in the arg list creates an empty output array (so long as you don't, y'know, pass any args). &amp;f creates a lambda reference to the passed-in block that can be called via f[] (normal Ruby convention is to use yield). a.map iterates over the input array without modifying it and is one character shorter than a.each. b-[p] strips out the nil values, equivalent to b.compact.