2005/05/26 sort -g vs. sort -n

When sorting numbers, you can use either -g or -n. I've noticed that Linux users without Unix experience tend to use -g, while old Unix folk are often unaware of that flag at all and continue to use -n. There is a difference between the two flags, although "man sort" doesn't explain it - "info sort" does a better job.

For ordinary numbers, -g and -n are identical, and although -g is slower, for small input sets it really doesn't matter. But when you mix in scientific notation, -n does not work:


Hate these ads?



$ cat t
 123
12
11
9
453
99
10e1
10e0
101
1
8.95
$ sort -n t
1
8.95
9
10e0
10e1
11
12
99
101
 123
453
$ sort -g t
1
8.95
9
10e0
11
12
99
10e1
101
 123
453


I mentioned that "sort -g" is slower. That's because it calls "strtod" to convert to double-precision floating point. When -n is used, sort simply aligns decimal points (real or assumed) and then does an ordinary string comparison. That's much faster, but of course it fails to handle scientific notation. The "faster" is, as noted, usually unimportant:



$ wc -l t
1321 t
$ time sort -g t > /dev/null



real    0m0.013s
user    0m0.012s
sys     0m0.002s
$ time sort -n t > /dev/null








real    0m0.009s
user    0m0.008s
sys     0m0.001s


You may have "-g" even if your man page doesn't mention it: my Mac OS X Tiger has -g but the man page doesn't mention it.

`







-
Google Friend Connect users can
comment on this page here


ad

Enter your email address for automatic notification of new posts here
(be sure to whitelist 'feedburner.com' if you use spam filtering)

Or use any RSS reader

Delivered by FeedBurner


Views for this page
Today This Week This Month This Year  Overall
215131,162 2,197

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

pavatar.jpg
More:
       - Shell




Unix/Linux Consultants


http://www.breakthru.com.au SCO (Openserver and Unixware), Unix, Solaris and Linux Consulting services including: Secure Networking Solutions; Linux based Firewalls; Backup Solutions; Secure Home to Office Network Setup; Phone, Remote and On-Site Support available - Satisfaction Guaranteed!


http://www.vss3.com SCO/Caldera OpenServer, Unixware & Linux. Tarantella & Non-stop Clustering


http://bcstechnology.net Full service Linux & UNIX systems integrator; Windows to UNIX/Linux Client-Server Specialist; Secure E-Mail & Website Hosting; Thoroughbred Software Developer; Custom Industrial Automation; Hardware & Electronics Experts; In Business Since 1985.



Twitter
  • Dec 3 14:01
    Just went out and added more bungee reinforcement. That ought to hold it..
  • Dec 3 13:58
    I'm second guessing myself on how I bungeed the cover on my golf cart for winter storage. Wondering if high wind could rip it off..









Change Congress