HyperTalk Reference
Home » HyperTalk Reference » Commands

Note: This is a work in progress and will be formatting errors. Read more about the project on the home page.


sort [sortDirection] [sortStyle] ¬
   by expression

sort [this] stack [sortDirection] ¬
   [sortStyle] by expression

sort [marked] cards [of this stack] ¬
   [sortDirection] [sortStyle] ¬
   by expression

sort bkgnd [sortDirection] ¬
   [sortStyle] by expression

sort [marked] cards of bkgnd ¬
   [sortDirection] [sortStyle ] ¬
   by expression

sort [chunks of] container ¬
   [sortDirection] [sortStyle] ¬
   [by expression]

where chunks are limited to either lines or items.

The first five forms of the sort command order all the cards in a stack or background by the value of expression, evaluated for each card in the stack or background.

The last form of the sort command (by expression ) sorts lines or items of a container by any expression. If you don’t specify, sort orders by lines. Before expression is evaluated for each line or item of the container, the local variable each is set to the contents of the chunk. (Click the Examples button to see syntax examples using each.)

For all forms of the sort command, the default sort direction is ascending, and the default sort style is text.

Sort direction ascending orders the sort elements—the value of the expression on each card or the lines or items in the container—from lower to higher values.

Sort direction descending orders the sort elements from higher to lower values.

Sort style text compares the sort elements based on their ASCII values:

"1" < "101" < "2" < "a" < "ab" < "b"

Note that neither case nor diacritical marks matter with the sort style text:

"apple" = "APPLE" = "äpplé"

Sort style numeric correctly sorts numbers. With sort style text,

"1" < "100" < "17" < "2"

The sort style numeric correctly sorts these values as:

"1" < "2" < "17" < "100"

The sort style dateTime orders the sort elements by their date or time format. (See the convert command for valid date and time formats.)

The sort style international correctly sorts non-English text containing diacritical marks and special characters based on the international resource installed in the current stack, the Home stack, HyperCard itself, or the System file.


-- Sort a container:

sort lines of card field 1
sort items of card field 1
sort lines of myVariable descending numeric
sort items of Message box
sort lines of the selection
sort lines of field 1 by last word of each -- by last word of each line
sort items of fld 1 descending numeric by word 2 of each
sort items of myList by the random of 2000 -- reorder myList randomly
sort lines of fld 1 numeric by length(cd fld each) -- if field 1
            -- contains names of cd flds, orders names according 
            -- to number of characters in each field.

-- Sort cards:

sort by bkgnd field "Name"
sort this stack by bkgnd field "Name"
sort stack by bkgnd field "Name"
sort marked cards of this stack by bkgnd field "Name"

sort this background by bkgnd field "Name"
sort background 2 by bkgnd field "Name"
sort marked cards of previous background by bkgnd field "Name"

sort descending by bkgnd field "Name"
sort descending international by bkgnd field "Name"

sort numeric by bkgnd field 2
sort numeric by the id of this card

sort by (bkgnd field "Last Name" & bkgnd field "First Name")
sort by mySortFunction()

Demo Script

on sortMe
   put line 10 to 13 of me into theLines
   select line 10 to 13 of me
   wait 30
   sort lines of the selection
   wait 1 second
   put theLines into line 10 to 13 of me
 end sortMe



One of the following:


One of the following:


HyperTalk DefinitionAny HyperTalk expression. All expressions evaluate to text, a number, or a constant.

For example:

"this is" && it
(3+2) = 5
the heapspace div 1024

Note: Formally, HyperCard distinguishes between factors (simple values) and expressions. The difference between factors and expressions matters only if you like to drop parentheses. Most functions take factors as their parameters, which is why length of 3 + 5 returns 6 and length of (3 + 5) returns 1. In short, always use parentheses to group things the way you want them to evaluate, and you won’t have to worry about the difference between factors and expressions.

AppleScript DefinitionAny series of words that has a value.

A HyperTalk expression that identifies a background by name, number, or id, using one of the following forms:

bkgnd id posInteger
bkgnd text -- name
bkgnd posInteger -- number
ordinal bkgnd
position bkgnd

For example:
bkgnd id 3894
bkgnd "Index"
bkgnd 1
prev bkgnd
previous bkgnd
next bkgnd
this bkgnd
first bkgnd
last bkgnd
middle bkgnd
any bkgnd
From within a background script, the term me refers to that background.
When a background receives a the message [the] target, that message is referring to the background.

See also: card

One of the following:


HyperTalk DefinitionA place where you can store and retrieve a value.

There are six types of containers in HyperCard: a variable, a button, a field, the selection, the Message box, and menus.

[the] selection
the Message box
btn 3
bg btn "Names"
card field 1
bkgnd field "Total"
menu 2
menu "Edit"
Additionally, you can refer to a button or field by its part number:

card part 1
last background part
AppleScript DefinitionAn object that contains one or more other objects, known as elements of the container. In a reference, the container specifies where to find an object. You specify containers with the reserved words of or in.

You can also use the possesive form ('s) to specify containers. For example, in

first window's name
the container is first window. The object it contains is a name property.

Related Topics

« show | HyperTalk Reference | start using »

Version 0.7b1 (March 24, 2022)

Made with Macintosh

Switch to Modern View