Skip to content

Searching and Sorting

Matt DeFano edited this page Mar 9, 2019 · 2 revisions

Sorting Cards | Sorting Containers | Searching

HyperTalk provides a powerful and flexible construct for sorting cards in a stack, or the items or lines in a container.

Sorting cards

The cards in a stack may be sorted by some expression using the syntax sort [the] cards [of this stack] by <expression>. For example:

-- Sort all cards based on the contents the first card field
sort the cards of this stack by the first card field

Cards are sorted by evaluating <expression> in the context of each card (<expression> being the first card field in the previous example). If the sort expression cannot be evaluated on each card (perhaps because one of the two cards has no fields) then the sort fails and no cards change position.

For example, if a stack has two cards, the contents of the first card field from each card will be compared; if the text of the second card's field is alphabetically before that of first card's, then the two card's will be switch positions.

When sorting things, you can specify how HyperTalk should interpret the data it compares. Possible formats are text or international (alphabetical order), numeric (numerical order), or dateTime (interpret values as dates or times and order them chronologically). In HyperCard, the international sort format had special behavior related to comparing diacriticals and whatnot, but in WyldCard its identical to the text format.

For example,

sort cards dateTime by field "Timestamp"    -- Sort chronologically
sort the cards numeric by bottomLine()      -- Sort numerically

By default, sorting re-orders things in ascending order (from first to last). You can reverse this order by specifying a sort direction:

sort cards descending numeric by the number of card buttons
sort cards ascending by the name of this card  

Additionally, a subset of cards identified by background or their marked property can be sorted without affecting the order of other cards in the stack (even if those cards are not contiguous).

sort the marked cards of this stack by the width of card button 2
sort cards of background id 2 by the random of 2

Sorting the contents of a container

The contents of a HyperTalk container (or a chunk of a container) can be sorted using the command sort <chunks> {of | in} <container> [<direction>] [<style>] [by <expression>] where:

  • <chunks> is [the] lines, [the] words, [the] items, or [the] chars (HyperCard only supports sorting by items or lines.)
  • <container> identifies a HyperTalk variable, part, or property
  • <direction> is either ascending or descending
  • <style> is text, numeric, dateTime or international (as described earlier)
  • <expression> is a HyperTalk expression in which each contains the value of each line or item being compared

Consider these examples,

sort the lines of menu "Edit"                                    -- alphabetize items of Edit menu
sort the items of the last line of myVar descending numeric
sort the lines of card field "Names" by the last word of each    -- sort names by last name
sort the lines of card field "Names" by the middle word of each  -- or by middle name

Searching

Use the find command to locate text anywhere in the stack or anywhere within a specified field. All searches are case insensitive.

The syntax for searching is find [<strategy>] [international] <factor> [in <field>] [of marked cards] where:

  • <strategy> is one of word, chars, whole, or string. When no <strategy> value is specified, whole is assumed. See the table below for a detailed description of each
  • international had special meaning in HyperCard (matching diphthongs and diacriticals); it has no meaning in WyldCard but is allowable in the syntax
  • <factor> is a single-term expression representing the text to find
  • <field> optionally specifies that the search should only take place within the single, specified field
  • of marked cards indicates that only marked cards should be searched. Has no effect if only a single field is being searched
Strategy Description
word Finds whole words in the searchable text. Search term should not contain any whitespace (if it is expected to match any text). Only whole words will match; substrings contained within a word will not.
chars Finds a substring that occurs entirely within the bounds of a word (does not cross word boundaries). Search term should not include whitespace (if it is expected to match anything).
whole Finds a substring that starts at the beginning of a word. Search term may contain whitespace, and search results may cross word boundaries (but will always start at a word boundary).
string Finds a substring occurring anywhere in the searchable text. Search term may including whitespace, and found-text may cross word boundaries.

Consider these examples,

find "Hyper"                             -- Searches the entire stack for occurrences of 'hyper'
find "Hyper" of marked cards             -- Searches only marked cards
find "Hyper" in background field "Card"  -- Searches only this one field
Clone this wiki locally