#!/usr/bin/perl

# ----------------------------------------------------------------
# John Kerl
# kerl.john.r@gmail.com
# 2003/07/20
#
# Sorts whitespace-delimited fields of input lines.  Contrast to the
# Unix sort command, which sorts entire lines.
#
# Sample input:
#   3 9 4 12 13 16 2 6 18 8 1
#   2 4 8 16 13 7 14 9 18 17 15 11 3 6 12 5 10 1
# Corresponding output from linesort:
#   1 12 13 16 18 2 3 4 6 8 9
#   1 10 11 12 13 14 15 16 17 18 2 3 4 5 6 7 8 9
# Corresponding output from linesort -n:
#   1 2 3 4 6 8 9 12 13 16 18
#   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# ----------------------------------------------------------------

$numerical = 0;
if (@ARGV) {
	if ($ARGV[0] eq "-n") {
		$numerical = 1;
		shift @ARGV;
	}
}

while ($line=<>) {
	chomp $line;
	@fields = split /\s+/, $line;

	if ($numerical) {
		@fields = sort {$a <=> $b} @fields;
	}
	else {
		@fields = sort @fields;
	}

	$i = 0;
	for my $field (@fields) {
		if ($i > 0) {
			print " ";
		}
		print $field;
		$i++;
	}
	print "\n";
}
