Open the Animals Starter File and click "Run".
In the Interactions Window on the right, type animals-table and hit "Enter" to see the default view of the table.

sort

Suppose we wanted to see the names of the animals in alphabetical order…​

The sort function takes in three pieces of information:

  1. A table

  2. A column we want to sort the table by (declared using a String)

  3. The order in which we want the column sorted (declared using a Boolean)

Test out these two expressions in the Interactions Area and record what you learn about ordering below:

  • sort​(​animals-table, "species", true​)

  • sort​(​animals-table, "species", false​)

1 true sorts the table…​

2 false sorts the table…​

Suppose we wanted to sort the animals-table by the weeks column to determine which animals were adopted quickest…​

3 Would you use true or false? Explain.

4 Test it out, and write your thinking about quantitative columns at the end of your explanations of true and false above.

5 Which animal(s) were adopted the quickest?

6 Some functions produce Numbers, some produce Strings, some produce Booleans. What did the sort function produce?

There are many other functions available to us in Pyret. We can describe them using contracts. The Contract for sort is:

# sort :: Table, String, Boolean -> Table

  • Each Contract begins with the function name: in this case sort

  • Lists the data types required to satisfy its Domain: in this case Table, String, Boolean

  • And then declares the data type of the Range it will return. in this case Table

  • Contracts can also be written with more detail, by adding variable names in the Domain:

# sort :: (Tabletable-name, Stringcolumn-name, Booleanorder) -> Table

Suppose we wanted to sort the animals-table by the legs column to determine which animals had the most legs…​

7 Fill in the blanks below with the code you’d use (We’ve put pieces of the Contract below each line to help you!):

function-name ( table-name :: Table, column-name :: String, order :: Boolean)

8 Which animal(s) had the most legs?

9 Think of another question you might answer quickly by sorting the table.

10 What code would you write to answer your question?

function-name ( table-name :: Table, column-name :: String, order :: Boolean)

These materials were developed partly through support of the National Science Foundation, (awards 1042210, 1535276, 1648684, 1738598, 2031479, and 1501927). CCbadge Bootstrap by the Bootstrap Community is licensed under a Creative Commons 4.0 Unported License. This license does not grant permission to run training or professional development. Offering training or professional development with materials substantially derived from Bootstrap must be approved in writing by a Bootstrap Director. Permissions beyond the scope of this license, such as to run training, may be available by contacting contact@BootstrapWorld.org.