Human sort
Sort the input stream of human readable numbers. Like 'sort-h' in the GNU coreutils.
IN
3T 1M 2G 500K
OUT
500K 1M 2G 3T
IN
5P 10b 500G 100M
OUT
10b 100M 500G 5P
IN
0K 1K 1 1023b 900b 2K 1M
OUT
0K 1 900b 1023b 1K 2K 1M
import Data.List (o#f)x y=f x`o`f y main=interact$unlines.sortBy(compare#h).lines h s=maybe(read s)(read(init s)*).lookup(last s).zip"bKMGTP"$iterate(*1024)1
import os k={'K':1024,'b':1,'M':1e6,'1':1,'G':1e9,'T':1e12,'P':1e15} m={} for s in os.read(0,9**9).split(): if"1"==s:m[1]='1' else:m[int(s[:-1])*k[s[-1]]]=s print'\n'.join(m[k] for k in sorted(m))