52 — Sorting — putting a list in order
A list stays in the order you wrote it. That is rarely the order you want to show — scores read best from the top, names read best in alphabetical order. There are only two sentences for putting a list in order.
Steps
Make a list and look at it.
joined by commashows a list on one line:
run it →set scores to list of 5, 3, 9, 1 show scores joined by comma5, 3, 9, 1— exactly as written.sortstands it up smallest first:
run it →set scores to list of 5, 3, 9, 1 sort scores show scores joined by commaYou get
1, 3, 5, 9. The list itself changes. From here onscoresstays in that order.Once it is standing, the two ends are your answer. Smallest first means the first item is the smallest and the last is the biggest:
run it →set scores to list of 5, 3, 9, 1 sort scores show the first of scores show the last of scores1and9.reverseturns it around. Reversing a sorted list gives biggest first:
run it →set scores to list of 5, 3, 9, 1 sort scores reverse scores show scores joined by comma9, 5, 3, 1. That is the order a score table is written in.Words stand up too. Numbers go smallest first, words go alphabetically:
run it →set names to list of Zoe, Mina, Ada sort names show names joined by commaYou get
Ada, Mina, Zoe.The whole thing:
run it →set scores to list of 5, 3, 9, 1 show as written show scores joined by comma sort scores show smallest first show scores joined by comma show the first of scores show the last of scores reverse scores show biggest first show scores joined by comma
Try it yourself
Put show the biggest of scores at the very top, before any sorting. It finds
the biggest without standing the list up at all. Sorting is what you do when
you want to see all of it in order.
What you learned
sort <list>stands a list up smallest first, or alphabetically for words.reverse <list>turns the current order around. After sorting, that is biggest first.- Both change the list itself. The original order is gone.
- After sorting,
the firstis the smallest andthe lastis the biggest.