let rec f xs =
try
f (input_line stdin::xs)
with End_of_file -> xs
in
let cmp x y =
let xl = String.length x in
let yl = String.length y in
if xl=yl
then String.compare x y
else xl-yl
in
List.map (fun x->Printf.printf "%s\n" x) (List.sort cmp (f []))
;;
Note that non-ascii characters in the above source code will be escaped (such as \x9f).