[comp.sys.mac.hypercard] Complex sorting in Supercard 1.5

mg2x+@andrew.cmu.edu (Michael Andrew Gelman) (06/18/91)

I'm working in SuperCard, but I think that HyperCard works the same way
(at least the older HyperCard).  I could be wrong, though, but I'll take
any clue.

I've got this stack that has four fields on it, F1...F4.  I need the
stack sorted by all the fields, that is first by F1, then by F2, etc.

Here's the trick.  F3 and F4 are sometimes text and sometimes numeric. 
If I do something like:

    sort by F1 & F2 & F3 & F4

I end up with the card that has F3 = "10" next to the card that has F3 =
"1".  It appears to be sorting by character, left to right.  Which is
right for text, but wrong for numbers.
I can't just sort numeric, because that doesn't do anything with text fields.

Has anyone run across this?  Can anyone see a solution?

Yours Alpha-numerically,
Mike Gelman  mg2x@andrew.cmu.edu
Programmer/Consultant -- Faculty Software Development Lab
College of Humanities and Social Sciences, Carnegie Mellon Univ.

stadler@Apple.COM (Andy Stadler) (06/18/91)

In article <ccLG7FC00WBL00cG5M@andrew.cmu.edu> mg2x+@andrew.cmu.edu
 (Michael Andrew Gelman) writes:

>I'm working in SuperCard, but I think that HyperCard works the same way
>(at least the older HyperCard).  I could be wrong, though, but I'll take
>any clue.
>
>I've got this stack that has four fields on it, F1...F4.  I need the
>stack sorted by all the fields, that is first by F1, then by F2, etc.
>
>  [...describes a method for multi-key sorts which doesn't work...]
>
>Has anyone run across this?  Can anyone see a solution?

As has been described on this board before, HyperCard supports a simple yet
powerful technique for multiple-key sorts.  Just sort and resort the stack,
using each key, in order of "importance" from last to first.

To sort your example, you would write:

  on multiSort
    sort by bkgnd field f4
    sort by bkgnd field f3
    sort by bkgnd field f2
    sort by bkgnd field f1
  end multiSort

Choice of sort modifiers (sort by numeric, sort descending, etc) is left to
the reader.

The key to this technique is that HyperCard religiously adheres to the concept
of a "stable sort".  This means that if you sort two items whos keys match,
the items will remain in the same relative order after sorting.  Not all sorting
algorithms do this, because for the most simple sorts, you don't have to.

I don't know anything about SuperCard 1.5.  But I certainly hope that SC's sort
is stable, and if it is, this technique should work just fine there, too.

Andy Stadler
Apple Computer, Inc.

gdavis@primate.wisc.edu (Gary Davis) (06/19/91)

From article <ccLG7FC00WBL00cG5M@andrew.cmu.edu>, by mg2x+@andrew.cmu.edu (Michael Andrew Gelman):
> I'm working in SuperCard, but I think that HyperCard works the same way
> (at least the older HyperCard).  I could be wrong, though, but I'll take
> any clue.
> 
> I've got this stack that has four fields on it, F1...F4.  I need the
> stack sorted by all the fields, that is first by F1, then by F2, etc.
> 
> Here's the trick.  F3 and F4 are sometimes text and sometimes numeric. 
> If I do something like:
> 
>     sort by F1 & F2 & F3 & F4
> 
> I end up with the card that has F3 = "10" next to the card that has F3 =
> "1".  It appears to be sorting by character, left to right.  Which is
> right for text, but wrong for numbers.
> I can't just sort numeric, because that doesn't do anything with text fields.
> Has anyone run across this?  Can anyone see a solution?

One way this could be done in HyperCard, and perhaps in SuperCard, would
be to take advantage of the fact the HC can sort on any arbitrary
expression. So, instead of sorting by F3, you could sort by a function
of F3. The function could look at the field's value and if it were
alphabetic, just return the value. If it were numeric, then the function
could calculate some appropriate set of characters which would place
the card in numeric order. If you want cards with numbers to come before
cards with text, then the function might return "aaa" & n for n < 10,
"aab" & n for 9 < n < 100 and so forth. (Oops, might need another "a",
if aardvark is in your text.) Other characters could be used if you
know where HC sorts them.

Gary Davis

gdavis@primate.wisc.edu (Gary Davis) (06/19/91)

From article <ccLG7FC00WBL00cG5M@andrew.cmu.edu>, by mg2x+@andrew.cmu.edu (Michael Andrew Gelman):
> I've got this stack that has four fields on it, F1...F4.  I need the
> stack sorted by all the fields, that is first by F1, then by F2, etc.
> 
> Here's the trick.  F3 and F4 are sometimes text and sometimes numeric. 

Oops, I just saw Andy Stadler's reply to your post pointing out that the
sort in HyperCard is stable. I had assumed that you meant that the fields
might contain a mixture of text and numbers at one time. If not, then
you wouldn't need to use the "function" sort I described.

Gary Davis