[comp.sys.mac.hypercard] Sorting by multiple keys

ben@rover.bsd.uchicago.edu (03/01/89)

I want to sort my cards by to "keys" for instance:
if I have a field date and a field name.
I want to sort my cards by date and all events happening on the same day 
to be sorted by name.

sort will not do this for me (as far as I can tell)

I can write an script to get the correct order for me but it seems
that I cannot change the postion of the cards myself.....

My only recourse seems to be to order the card numbers the way I want
(ie. sort the cards and store the sort order in a object) and then 
change the script of the "next", "prev" arrows to go to the correct cards
this is a kludge and I  want a better way!!

Does one exist???

Ben Herman

jdevoto@Apple.COM (Jeanne A. E. DeVoto) (03/01/89)

In article <2053@tank.uchicago.edu> ben@rover.bsd.uchicago.edu writes:
>I want to sort my cards by to "keys" for instance:
>if I have a field date and a field name.
>I want to sort my cards by date and all events happening on the same day 
>to be sorted by name.

HyperTalk's sort algorithm is of the type called "stable". That is, a
sort will not change the order of cards that have the same sort key.

What this means is that you can accomplish what you want by using multiple
sorts. In this case, you would first sort by the "Name" field, then
sort by the "Date" field. The second sort leaves the name order of cards
that have the same date untouched. The general rule is to sort by least
significant key first. The HyperTalk for this example might look like
this:
  on sortMyStack
    put "Now sorting stack..."
    sort by background field "Name"
    sort dateTime by background field "Date"
    hide message box
  end sortMyStack

jeanne a. e. devoto
jdevoto@apple.com

ns@cat.cmu.edu (Nicholas Spies) (03/02/89)

In article <2053@tank.uchicago.edu> ben@rover.bsd.uchicago.edu writes:
>I want to sort my cards by two "keys"...

You can sort by concatenating the contents of fields
(e.g. sort by field "a" & field "b")

In fact, you can sort by the result of an expression or function, so long as
the expression or function can be applied to each card. Thus

sort by field "xx" is empty & field "xx" -- will sort by field "xx", but put
empty field "xx" cards at the end (rather than the beginning) of the stack.
Why? Because -- field "xx" is empty -- evaluates to either TRUE or FALSE.

The same technique can be used using functions.


-- 
Nicholas Spies			ns@cat.cmu.edu.arpa
Center for Design of Educational Computing
Carnegie Mellon University
--