Re: Combining single-key sorts to form concatenated-key sort

From: James Chapman <Jim.Chapman_at_nospam.ncr.com>
Date: Mon, 5 Feb 2001 16:27:50 -0800
Message-ID: <3a7f4475_at_rpc1284.daytonoh.ncr.com>


This problem is a subset of that faced by every SQL database engine. In addition to multiple keys and data types, they must handle nulls, different
character collations, and both ascending and descending order.

I am aware of 2 general solutions:

  1. Transform the sort keys into a single byte string, whose ordering, when evaluated in standard left-to-right fashion, will be identical to that produced by evaluating the original keys. The transformation entails such things as (a) padding variable length keys out to uniform length, right-justifying numerics, (b) inserting extra bits for nullable fields, and (c) inverting values to be sorted in descending order.
  2. Utilize a sort function that provides a call-back interface for key comparison.

"Frog2" <FrogRemailer_at_NoReply.Invalid> wrote in message news:NJB1JYZE36925.3276388889_at_frog.nyarlatheotep.org...
> I'm trying to sort some records. Each record consists of three data fields
 I wish to use
> as a concatenated key for sorting purposes and some detail fields. All
 three key fields
> are alphanumeric. All key fields in every record will always contain a
 value (no NULLs).
>
> Here's the problem: the language I'm using has a sort function but that
 function will
> only let me sort on *one* field of my choice, not all three at once. So
 how can I achieve
> a primary - secondary - tertiary sort of these records. Presumably there's
 some scalable
> way to sort/merge each lower key with the key above it (or perhaps the
 reverse, sort from
> primary key down to lowest-order key).
>
> What I've come with so far is make a list of each key field's unique
 values
> unique_p , unique_s, unique_t
> sorted in the order I want. But then how could I use these to extract the
 records in
> correct overall sorted order?
>
> One subtlty - one or more key fields will contain only numbers. So if I
 try an "strcat-'em
> all-together" solution I'll get bit by the "10-sorts-after-1-not-2"
 problem.
>
> Any ideas on how to do this? Please post any replies back to the list so
 that others may
> learn.
>
>
> TIA,
>
> Mike
>
Received on Tue Feb 06 2001 - 01:27:50 CET

Original text of this message