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
Login to submit solution


7 symbols bash
Fri Dec 27 03:28:52 2013 Logioniz
sort -h
7 symbols bash
Fri Feb 7 12:16:30 2014 maksim
sort -h
8 symbols bash
Fri Feb 7 12:16:55 2014 maksim
sort -h
84 symbols ruby2.0
Wed Nov 23 17:05:21 2016 Sait2000
b=1
K=1024
M=K*K
G=K*M
T=K*G
P=K*T
puts$<.lines.sort_by{|l|eval l.sub /\p{L}/,'*\0'}
87 symbols ruby2.0
Thu Nov 3 19:49:52 2016 Sait2000
b=1
K=1024
M=K*K
G=K*M
T=K*G
P=K*T
puts$<.lines.sort_by{|l|eval l.chomp.sub /\D/,'*\0'}
88 symbols ruby2.0
Thu Nov 3 19:49:07 2016 Sait2000
b=1
K=1024
M=K*K
G=K*M
T=K*G
P=K*T
puts$<.lines.sort_by{|l|eval l.chomp.sub(/\D/,'*\0')}
95 symbols ruby2.0
Sun Oct 5 18:59:35 2014 fl00r
N=1024
b=1
K=N*b
M=N*K
G=N*M
T=N*G
P=N*T
puts$<.lines.sort_by{|l|eval(l.chomp.sub(/\D/,'*\0'))}
95 symbols ruby2.0
Sun Oct 5 18:59:47 2014 fl00r
N=1024
b=1
K=N*b
M=N*K
G=N*M
T=N*G
P=N*T
puts$<.lines.sort_by{|l|eval(l.chomp.sub(/\D/,'*\0'))}
157 symbols haskell
Wed Mar 18 11:03:49 2015 YoshikuniJujo
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
198 symbols python2
Wed Aug 13 01:03:56 2014 PFischbeck
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))

View all solutions