HyperCard: A Sorted Tale

MacDigest

The more familiar you are with traditional data base programs, the more knocked-out you will be with the way HyperCard handles certain fields. HyperCard’s ability to sort and manipulate a single multi-line field as if it were made up of individual fields is one such trick. Using a single multi-line field allows great flexibility in entering data, a good example of which can be seen in the Address stack that comes with HyperCard.

With this flexibility comes some trade-offs, however. If the first line of a multi-line field contains the full name, how do you sort that card? You can sort it in straight alphabetical order, but that only works if the first line contains a business name, or if you enter names in the Lastname, Firstname format. Even if you don’t mind entering people’s names lastname first (I hate it), what happens if you wish to mix some company names in as well?

The more you think about it the more you see that there are some advantages to breaking up your large multi-line field into several smaller fields. After a lot of thought, I choose to set up my address stack with a separate field for first name, last name, and company. This gets me neatly around questions of what to do with friends who have MD or Jr after their name.

The most natural place to start experimenting with sorting the stack is with the simplest script. One minimal script for a sort button would be:

	on mouseUp
		sort by company & lastname & firstname
	end mouseUp
				

The ampersand tells HyperCard to concatenate the fields. Obviously, I have named the fields in my address stack, although I could have referred to them as field 3, field 2, and field 1.

After clicking on this sort button, it becomes apparent that this script does not produce quite the results we had hoped for. When looking for our favorite MacSoftware customer support person Vic Johns’ phone number, we find that his card has been placed after the card of fellow employee Bill Johnston. This is because the concatenation operator creates a sort field that looks like this:

MacSoftwareJohnstonBill
MacSoftwareJohnsVic

The way we concatenated the text field strings together, we have introduced an error into the way HyperCard perceives and sorts our information. We can easily fix this, however, by changing the button script to read:

	on mouseUp
		sort by company && lastname && firstname
	end mouseUp
				

The double ampersand, you remember, tells HyperCard to concatenate the text strings with a space in between. When we sort the stack this time, we find that Vic and Bill are now in the correct order:

MacSoftware Johns Vic
MacSoftware Johnston Bill

Say for the sake of argument that you have a friend in your address stack who does not have a company name (I often have one card for the person’s home address and one card for the person at his company address). Even if your friend’s name was Ziggy Zipperneck, HyperCard would sort him right to the front of the stack. If I had separate cards for ol’ Ziggy's home and work addresses, the sorted stack would look like this:

 Zipperneck Ziggy
MacSoftware Johns Vic
MacSoftware Johnston Bill
MacSoftware Zipperneck Ziggy

Ziggy’s company address card sorted fine, but the home address card is at the top of the stack instead of at the bottom. What happened? Without a company name the first character in the sort string is a space, which alphabetically comes before any of the letters of the alphabet. This leaves you with two distinct sections to your stack; one section for those entries with company names and one section for those without.

I prefer to have people’s names alphabetized right in among the company names rather than having two separate sections. Therefore, the button script I use for sorting my address stack is a compromise between these two extremes:

	on mouseUp
		sort by company & lastname && firstname
	end mouseUp
				

HyperCard now will intersperse company cards and people cards properly:

MacSoftware Johns Vic
MacSoftware Johnston Bill
MacSoftware Zipperneck Ziggy
Zipperneck Ziggy

At least, it will with one exception, and that is when the last name is not entered. I have two cards in my address stack for which I do not have a last name for the person and there is no company name. Because of the double ampersand in the sort script, these cards are sorted to the top of the stack:

Hilda
Stella
MacSoftware Johns Vic
MacSoftware Johnston Bill
MacSoftware Zipperneck Ziggy
Zipperneck Ziggy

Although it might be nice to have them sorted by first name alone (the first button script listed above will do this), I actually don’t mind having these garbage cards show up at the top of the stack. They serve to remind me that I need to get the last names for these entries so the cards will sort correctly. If these cards were sorted to the middle of my address stack they would be out of sight and therefore out of mind.

With my stack sorting the way I want it, I make sure I always know when it needs re-sorting by adding new cards only at the end of the stack. On opening the stack, I can click on the Go to last card button and immediately see if the stack needs housekeeping. By the way, last month’s tip on compacting the stack can be a big space saver, but if you plan on sorting your stack do so before you compact the stack. Otherwise, running the sort will regenerate some of the free space that you eliminated when you compacted the stack.