§16.11. Sorting
The three ways to sort a table correspond loosely to the three different orders in which tables can be repeated through. First:
sort (table name) in random order
This phrase rearranges the rows of the given table so that the non-blank rows occur at the top, in a uniformly random order, and any blank rows at the bottom. Example:
sort the Table of Recent Monarchs in random order;
Secondly:
sort (table name) in (table column) order
This phrase rearranges the rows of the given table so that the non-blank rows occur at the top, so that the given column has ascending order, and any blank rows at the bottom. Example:
sort the Table of Recent Monarchs in accession order;
Ascending order means 1 up to 10, say, or A up to Z, with blank values coming last.
sort (table name) in reverse (table column) order
This phrase rearranges the rows of the given table so that the non-blank rows occur at the top, so that the given column has descending order, and any blank rows at the bottom. Example:
sort the Table of Recent Monarchs in reverse name order;
Descending order means 10 down to 1, say, or Z down to A, with blank values coming last.
How sorting is done depends on the contents of the column being sorted on. If it holds numbers then numerical order is used, with 2 coming before 7, and so on. (And similarly for real numbers, though the existence of infinities makes this more interesting.) If times are sorted then they are sorted from midnight to midnight, following the "is greater than" relation, not with 4 AM as the zero point, as with "is after".
If text is sorted then alphabetical order is used, though this doesn't always come out the way you might expect, because upper case and lower case letters are treated as different: A-Z come before a-z, and accented letters such as é come after the regular alphabet. (What's happening here is that Inform is sorting on raw character values, not performing the full Unicode collation algorithm, which would be too slow at run-time.)
Note that blank values will always be placed below non-blank ones, and entirely blank rows last of all. This is true even if we use "reverse".
The method of sorting is "stable", that is, if two rows have the same value then they will stay the same way round in the sorted table, rather than being swapped over. For example, if we sort this into reverse index order:
Index | Comment |
1 | "Originally row 1" |
2 | "Originally row 2" |
2 | "Originally row 3" |
3 | "Originally row 4" |
then we get
Index | Comment |
3 | "Originally row 4" |
2 | "Originally row 2" |
2 | "Originally row 3" |
1 | "Originally row 1" |
As a result note that repeating through this sorted table goes through the original rows in order 4, 2, 3, 1; whereas repeating through the original table in reverse order goes through in order 4, 3, 2, 1. (This is all to explain the word "loosely" in the opening sentence of this section.)
Suppose we want a deck of cards which the player can shuffle and draw from. Our first (rather tedious) task is merely to set up the deck as a table:
"Jokers Wild"
Suit is a kind of value. The suits are hearts, clubs, diamonds, and spades.
Table of Cards
suit | value |
diamonds | 1 |
diamonds | 2 |
diamonds | 3 |
diamonds | 4 |
diamonds | 5 |
diamonds | 6 |
diamonds | 7 |
diamonds | 8 |
diamonds | 9 |
diamonds | 10 |
diamonds | 11 |
diamonds | 12 |
diamonds | 13 |
spades | 1 |
spades | 2 |
spades | 3 |
spades | 4 |
spades | 5 |
spades | 6 |
spades | 7 |
spades | 8 |
spades | 9 |
spades | 10 |
spades | 11 |
spades | 12 |
spades | 13 |
hearts | 1 |
hearts | 2 |
hearts | 3 |
hearts | 4 |
hearts | 5 |
hearts | 6 |
hearts | 7 |
hearts | 8 |
hearts | 9 |
hearts | 10 |
hearts | 11 |
hearts | 12 |
hearts | 13 |
clubs | 1 |
clubs | 2 |
clubs | 3 |
clubs | 4 |
clubs | 5 |
clubs | 6 |
clubs | 7 |
clubs | 8 |
clubs | 9 |
clubs | 10 |
clubs | 11 |
clubs | 12 |
clubs | 13 |
We're going to describe the higher numbers as face cards, so it helps to write a new "to say" phrase.
To say (count - a number) as a card value:
choose row with a value of count in the Table of Value Names;
say "[term entry]".
Table of Value Names
value | term |
1 | "ace" |
2 | "deuce" |
3 | "three" |
4 | "four" |
5 | "five" |
6 | "six" |
7 | "seven" |
8 | "eight" |
9 | "nine" |
10 | "ten" |
11 | "jack" |
12 | "queen" |
13 | "king" |
Now we get the shuffling of the deck from "sort in random order", so:
Understand "shuffle" as shuffling. Shuffling is an action applying to nothing.
Carry out shuffling:
sort the Table of Cards in random order;
say "You expertly rearrange the cards.".
When play begins:
sort the Table of Cards in random order.
This will continue to work properly even as the deck is partially depleted. Speaking of which, suppose we want the player to be able to toss the cards one-by-one into a hat. They are going to need to be removed from the deck, so:
Understand "toss" or "toss a card" or "toss card" as tossing.
Tossing is an action applying to nothing.
Check tossing:
if the number of filled rows in the Table of Cards is 0, say "The deck is empty." instead.
Carry out tossing:
repeat through the Table of Cards:
let new value be value entry;
let new suit be suit entry;
say "You throw the [value entry as a card value] of [suit entry] at the top hat, and [if a random chance of 1 in 3 succeeds]hit[otherwise]miss[end if].";
blank out the whole row;
rule succeeds.
If we wanted to simulate a slightly more stimulating game, we could instead have a second table to represent the player's hand of cards and record each card drawn. That would get long for the purposes of example, however, so instead we will just admit that the player's life is an empty husk of existence:
The Empty Room is a room. "It has come to this: sitting on the bare floor of Lulu's apartment with nothing to amuse you but a deck of cards and the top hat from last year's act. You reckon [the number of filled rows in the Table of Cards in words] cardtosses are all that stand between you and the utter pointlessness of existence.
Once again you curse Lulu for running off with that joker."
The player is carrying the deck of cards. The top hat is an open container in the Empty Room. It is scenery.
Test me with "toss / again / again / again / again / again / again / again".
|  ExampleJokers Wild A deck of cards which can be shuffled and dealt from.
|
Suppose we want a deck of cards which the player can shuffle and draw from. Our first (rather tedious) task is merely to set up the deck as a table:
"Jokers Wild"
Suit is a kind of value. The suits are hearts, clubs, diamonds, and spades.
Table of Cards
suit | value |
diamonds | 1 |
diamonds | 2 |
diamonds | 3 |
diamonds | 4 |
diamonds | 5 |
diamonds | 6 |
diamonds | 7 |
diamonds | 8 |
diamonds | 9 |
diamonds | 10 |
diamonds | 11 |
diamonds | 12 |
diamonds | 13 |
spades | 1 |
spades | 2 |
spades | 3 |
spades | 4 |
spades | 5 |
spades | 6 |
spades | 7 |
spades | 8 |
spades | 9 |
spades | 10 |
spades | 11 |
spades | 12 |
spades | 13 |
hearts | 1 |
hearts | 2 |
hearts | 3 |
hearts | 4 |
hearts | 5 |
hearts | 6 |
hearts | 7 |
hearts | 8 |
hearts | 9 |
hearts | 10 |
hearts | 11 |
hearts | 12 |
hearts | 13 |
clubs | 1 |
clubs | 2 |
clubs | 3 |
clubs | 4 |
clubs | 5 |
clubs | 6 |
clubs | 7 |
clubs | 8 |
clubs | 9 |
clubs | 10 |
clubs | 11 |
clubs | 12 |
clubs | 13 |
We're going to describe the higher numbers as face cards, so it helps to write a new "to say" phrase.
To say (count - a number) as a card value:
choose row with a value of count in the Table of Value Names;
say "[term entry]".
Table of Value Names
value | term |
1 | "ace" |
2 | "deuce" |
3 | "three" |
4 | "four" |
5 | "five" |
6 | "six" |
7 | "seven" |
8 | "eight" |
9 | "nine" |
10 | "ten" |
11 | "jack" |
12 | "queen" |
13 | "king" |
Now we get the shuffling of the deck from "sort in random order", so:
Understand "shuffle" as shuffling. Shuffling is an action applying to nothing.
Carry out shuffling:
sort the Table of Cards in random order;
say "You expertly rearrange the cards.".
When play begins:
sort the Table of Cards in random order.
This will continue to work properly even as the deck is partially depleted. Speaking of which, suppose we want the player to be able to toss the cards one-by-one into a hat. They are going to need to be removed from the deck, so:
Understand "toss" or "toss a card" or "toss card" as tossing.
Tossing is an action applying to nothing.
Check tossing:
if the number of filled rows in the Table of Cards is 0, say "The deck is empty." instead.
Carry out tossing:
repeat through the Table of Cards:
let new value be value entry;
let new suit be suit entry;
say "You throw the [value entry as a card value] of [suit entry] at the top hat, and [if a random chance of 1 in 3 succeeds]hit[otherwise]miss[end if].";
blank out the whole row;
rule succeeds.
If we wanted to simulate a slightly more stimulating game, we could instead have a second table to represent the player's hand of cards and record each card drawn. That would get long for the purposes of example, however, so instead we will just admit that the player's life is an empty husk of existence:
The Empty Room is a room. "It has come to this: sitting on the bare floor of Lulu's apartment with nothing to amuse you but a deck of cards and the top hat from last year's act. You reckon [the number of filled rows in the Table of Cards in words] cardtosses are all that stand between you and the utter pointlessness of existence.
Once again you curse Lulu for running off with that joker."
The player is carrying the deck of cards. The top hat is an open container in the Empty Room. It is scenery.
Test me with "toss / again / again / again / again / again / again / again".
Suppose we want a deck of cards which the player can shuffle and draw from. Our first (rather tedious) task is merely to set up the deck as a table:
"Jokers Wild"
Suit is a kind of value. The suits are hearts, clubs, diamonds, and spades.
Table of Cards
suit | value |
diamonds | 1 |
diamonds | 2 |
diamonds | 3 |
diamonds | 4 |
diamonds | 5 |
diamonds | 6 |
diamonds | 7 |
diamonds | 8 |
diamonds | 9 |
diamonds | 10 |
diamonds | 11 |
diamonds | 12 |
diamonds | 13 |
spades | 1 |
spades | 2 |
spades | 3 |
spades | 4 |
spades | 5 |
spades | 6 |
spades | 7 |
spades | 8 |
spades | 9 |
spades | 10 |
spades | 11 |
spades | 12 |
spades | 13 |
hearts | 1 |
hearts | 2 |
hearts | 3 |
hearts | 4 |
hearts | 5 |
hearts | 6 |
hearts | 7 |
hearts | 8 |
hearts | 9 |
hearts | 10 |
hearts | 11 |
hearts | 12 |
hearts | 13 |
clubs | 1 |
clubs | 2 |
clubs | 3 |
clubs | 4 |
clubs | 5 |
clubs | 6 |
clubs | 7 |
clubs | 8 |
clubs | 9 |
clubs | 10 |
clubs | 11 |
clubs | 12 |
clubs | 13 |
We're going to describe the higher numbers as face cards, so it helps to write a new "to say" phrase.
To say (count - a number) as a card value:
choose row with a value of count in the Table of Value Names;
say "[term entry]".
Table of Value Names
value | term |
1 | "ace" |
2 | "deuce" |
3 | "three" |
4 | "four" |
5 | "five" |
6 | "six" |
7 | "seven" |
8 | "eight" |
9 | "nine" |
10 | "ten" |
11 | "jack" |
12 | "queen" |
13 | "king" |
Now we get the shuffling of the deck from "sort in random order", so:
Understand "shuffle" as shuffling. Shuffling is an action applying to nothing.
Carry out shuffling:
sort the Table of Cards in random order;
say "You expertly rearrange the cards.".
When play begins:
sort the Table of Cards in random order.
This will continue to work properly even as the deck is partially depleted. Speaking of which, suppose we want the player to be able to toss the cards one-by-one into a hat. They are going to need to be removed from the deck, so:
Understand "toss" or "toss a card" or "toss card" as tossing.
Tossing is an action applying to nothing.
Check tossing:
if the number of filled rows in the Table of Cards is 0, say "The deck is empty." instead.
Carry out tossing:
repeat through the Table of Cards:
let new value be value entry;
let new suit be suit entry;
say "You throw the [value entry as a card value] of [suit entry] at the top hat, and [if a random chance of 1 in 3 succeeds]hit[otherwise]miss[end if].";
blank out the whole row;
rule succeeds.
If we wanted to simulate a slightly more stimulating game, we could instead have a second table to represent the player's hand of cards and record each card drawn. That would get long for the purposes of example, however, so instead we will just admit that the player's life is an empty husk of existence:
The Empty Room is a room. "It has come to this: sitting on the bare floor of Lulu's apartment with nothing to amuse you but a deck of cards and the top hat from last year's act. You reckon [the number of filled rows in the Table of Cards in words] cardtosses are all that stand between you and the utter pointlessness of existence.
Once again you curse Lulu for running off with that joker."
The player is carrying the deck of cards. The top hat is an open container in the Empty Room. It is scenery.
Test me with "toss / again / again / again / again / again / again / again".
|