[comp.sys.mac.hypercard] sorting problems

stadler@Apple.COM (Andy Stadler) (03/28/91)

In article <120@lake.scar.toronto.edu> barek@lake.scar.toronto.edu
 (William Barek) writes:
>
>   Secondly, I could not belive that sorting is almost useless (please no
>   flames). Why. Have you ever tried to sort 2 fields ( one text, other
>   numeric) or lines in a field that have two columns one is text other is
>   numeric). To me it is a very basic operation, why this was missed or am
>   I missing something?

Both of these tricks are easy.  To sort using multiple keys, just sort the
stack multiple times, starting with the minor sort key and finishing with the
major sort key.  For example, if you have a stack with first name and last name
fields, you would sort it like this:

  sort by bkgnd field "first name"
  sort by bkgnd field "last name"

HyperCard's sort is "stable" which means that if two keys are identical, the
cards will never be swapped.  This assures that multi-key sorts work correctly.

To sort by particular elements of a field, just use "item n" to determine the
location of the elements.  In the previous example, suppose you have a single
"name" field in which the names are entered as "lastname, firstname".  You 
could sort it with these two commands:

  sort by item 2 of bkgnd field "name"   -- sorts first names
  sort by item 1 of bkgnd field "name"   -- now sort the last names

You can use all kinds of selectors to specify your sort key.  We call them
"chunk expressions."  A chunk can be chars, items, words, lines, or any
combination of the above.  To build really complex things, use a creation pass
to go through the stack, extract exactly what you want, and put it in another
field;  then sort the stack using that field.  But you almost never need to
go this far, in practice.

There are many other options in the sort command, such as sort style [text/
numeric/international] and sort direction [ascending/descending].  In addition
you can sort only those cards meeting certain criteria [sort marked cards] and
you can sort items within a container.

I'm sorry you aren't happy with 2.0, but I suspect you haven't taken the time
to really dig into its capabilities.  It's really quite powerful!

Andy Stadler
HyperCard IIGS Team
Apple Computer, Inc.

fwb@pollux.tmc.edu (Fred Brehm) (03/28/91)

In article <120@lake.scar.toronto.edu> barek@lake.scar.toronto.edu
 (William Barek) writes:
>
>   Secondly, I could not belive that sorting is almost useless (please no
>   flames). Why. Have you ever tried to sort 2 fields ( one text, other
>   numeric) or lines in a field that have two columns one is text other is
>   numeric). To me it is a very basic operation, why this was missed or am
>   I missing something?

In article <50903@apple.Apple.COM> stadler@Apple.COM (Andy Stadler) answers:
>Both of these tricks are easy.  To sort using multiple keys, just sort the
>stack multiple times, starting with the minor sort key and finishing with the
>major sort key.  For example, if you have a stack with first name and last name
>fields, you would sort it like this:
[explanation deleted]

I think you answered the wrong question.  A stable sort makes sorting by
multiple keys possible.  However, he did not ask about sorting cards.  One
of Mr. Barek's requests could be fulfilled by a command like

  sort lines of container by item 2 descending numeric
                          ^^^^^^^^^
but HyperCard 2.0v2 doesn't have this.

Another request seems to be sorting of two separate fields. I'm not sure
exactly what he means by this, but I'll guess that he would like to consider
line i of each of the two fields to be somehow considered as one record.

There are work-arounds to both of these things using HyperTalk, but they
are pretty ugly.  An XFCN or XCMD would probably work much faster.

Fred
-- 
Frederic W. Brehm	Siemens Corporate Research	Princeton, NJ
fwb@demon.siemens.com	-or-	...!princeton!siemens!demon!fwb

jkc@Apple.COM (John Kevin Calhoun) (03/29/91)

In article <56786@siemens.siemens.com> fwb@pollux.tmc.edu (Fred Brehm) writes:
>
>One of Mr. Barek's requests could be fulfilled by a command like
>
>  sort lines of container by item 2 descending numeric
>                          ^^^^^^^^^
>but HyperCard 2.0v2 doesn't have this.
>
>There are work-arounds to both of these things using HyperTalk, but they
>are pretty ugly.  An XFCN or XCMD would probably work much faster.

There's an XFCN called SortFieldByItem by Roger Brown in the Dartmouth XCMDs
that sorts the lines of fields alphabetically by any item within the line.
Source code is included.

Kevin Calhoun
HyperCard Team
Apple Computer, Inc.