sort oddity

Bradley C. Kuszmaul (bradley@ee.yale.edu)
Wed, 24 Mar 1999 12:16:34 -0500

> From: Ken Lai (min-ken.lai@yale.edu)
> Date: Fri, 5 Mar 1999 16:59:07 -0500 (EST)
>
> Try this:
> ps aux | tail +2 | sort +1
>
> ... is out of order. Anyone happen to know why? ....

My answer to this has two parts: What you should have done, and why
you get the apparently strange results you got.

(1) You should have specified that you wanted numeric sorting. E.g.,
Do this:
ps aux | tail +2 | sort +1 -n

(2) Why did you get those funny results? If you read the sort man
page, you find that by default, the separator is whitespace, but the
leading whitespace is actually included in each field.
So for example, given these two lines (shown in "sort +n" order)

nobody 32298 0.0 0.6 1536 848 ? S Mar 22 0:00 httpd
bradley 8865 0.0 0.5 1776 712 p3 S Mar 15 0:00 -bin/tcsh -i
bradley 11640 0.0 0.5 1512 656 ? S Mar 16 0:00 /home/bradley/wp/shb
i

The first line is broken into the fields "nobody" and " 32298".
The second line is broken into the fields "bradley" and " 8865".
The third line is broken into the fields "bradley" and " 11640".

Yes, it turns out that strcmp(" 32298"," 8865")<0 and
strcmp(" 8865"," 11640")<0

The funny sort order is because there is a different amount of
whitespace in field 1 of the second line as compared to field 1 of
the third line.

The lesson: RTFM! (No slight intended.) Unix man pages are notorious
for the fact that you must read them extremely carefully. Sometimes
they are wrong, but at least the man page is accurate in this case.

Cheers,
Prof. Kuszmaul