Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!mimsy!umd5!brl-adm!brl-smoke!gwyn From: gwyn@brl-smoke.ARPA (Doug Gwyn ) Newsgroups: comp.unix.questions Subject: Re: Multiple Field Sorts in UNIX(tm) Message-ID: <6152@brl-smoke.ARPA> Date: Thu, 23-Jul-87 01:47:07 EDT Article-I.D.: brl-smok.6152 Posted: Thu Jul 23 01:47:07 1987 Date-Received: Sat, 25-Jul-87 05:48:10 EDT References: <2459@whuts.UUCP> Reply-To: gwyn@brl.arpa (Doug Gwyn (VLD/VMB)) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 16 In article <2459@whuts.UUCP> tes@whuts.UUCP (STERKEL) writes: > I need a multiple field sort that maintains sub-field order. The standard UNIX "sort" utility can be asked to compare multiple keys, which will probably do what you want. If the original record order must be retained even when ALL keys match, then add an additional key field consisting of sequence numbers, sort on the desired multiple keys followed by the sequence number key, and finally strip off the sequence numbers. Sorts that inherently preserve original record order when keys (all) match are called "stable". As you discovered, the n-way merge sorting used by the UNIX "sort" utility is not stable. My favorite in-core stable sorting technique is "list merge" as described in Knuth Vol. 3, which is a good place to start if you need to learn more about sorting techniques.