Sorting
Putting a list in order is called sorting, and Lua does it for you with table.sort. It sorts numbers from the smallest to the largest:
local weights = {8, 12, 7, 15, 10}
table.sort(weights)
print(table.concat(weights, " "))This prints:
7 8 10 12 15table.sort changes the list itself: the items move to new places. It doesn't return anything, so don't store its result. After local sorted = table.sort(weights), sorted is nil. Just write table.sort(weights) on a line of its own and keep using weights.
In a sorted list, the smallest item comes first, weights[1], and the largest one last, weights[#weights]. That's handy, but the original order is lost. If you need to know where an item was, for example which day was the best, use the loop from the previous exercise instead. To get the items from the largest to the smallest, go through the sorted list backwards:
local weights = {8, 12, 7, 15, 10}
table.sort(weights)
for i = #weights, 1, -1 do
io.write(weights[i], " ")
end
print()This prints:
15 12 10 8 7 table.sort can also sort from the largest directly, but for that, you have to give it a function that compares two items. You'll write such functions in the lesson on functions.
Texts can be sorted too, but not quite as in a dictionary. Lua compares them character by character, by the characters' codes, and in those codes, all capital letters come before all small ones:
local crops = {"potato", "carrot", "Pumpkin", "wheat", "corn"}
table.sort(crops)
print(table.concat(crops, ", "))This prints:
Pumpkin, carrot, corn, potato, wheat"Pumpkin" ended up first only because of its capital P. If you want the order of a dictionary, write all the texts the same way, for example in small letters. (In the lesson on strings, you'll learn to change capital letters to small ones in code.) Letters with accents, such as é, have even bigger codes, so they end up after z.
Numbers written as texts are sorted as texts, character by character: {"10", "9", "100"} becomes "10", "100", "9", because the character 1 comes before 9. Keep numbers as numbers. And a list with both numbers and texts can't be sorted at all, because Lua can't compare a number with a text:
local mixed = {3, "carrot"}
table.sort(mixed)attempt to compare string with number
stack traceback:
[C]: in function 'table.sort'
main.lua:2: in main chunkThis message has no main.lua: and line number at the start, because the error happened inside table.sort. You'll find the line of your program in the traceback: main.lua:2: in main chunk.
Instructions
At the end of the program, sort weights with table.sort.
Print the sorted weights with table.concat, separated by spaces: Sorted: 7 8 9 10 12 15.
Print the three heaviest pumpkins, starting with the heaviest one. Go through the end of the sorted list backwards: Top 3: 15 12 10.
Sort crops too, and print them: Crops: followed by table.concat(crops, ", "). Pumpkin comes first because of its capital letter. Fix the list at the top of the program so that the crops are in alphabetical order: Crops: carrot, corn, potato, pumpkin, wheat.
Start programming for free
10/11