This Monday’s rota is settled: Sam’s leave is handled, Lena sits straight after Dev, and the request to remove Rui, who isn’t on the rota at all, no longer takes Marta off by mistake. The Monday script has one more reader to serve. HR keeps a wiki page of everyone on the out-of-hours rota, and they want the names in alphabetical order.
The rota’s order, though, matters. It says who carries the pager next, so the wiki list mustn’t disturb it. In this lesson, you’ll port Python’s two ways of sorting a list and see which one leaves the rota alone. Start with the one the Python script uses for the wiki.
Sort a Copy With .toSorted()
The Python script builds the wiki list with sorted(), which returns a new list and leaves the rota as it was:
rota = ["Dev", "Lena", "Ines", "Kofi", "Bea", "Marta"]
wiki = sorted(rota)
print(f"Wiki list: {', '.join(wiki)}")
print(f"Rota: {', '.join(rota)}")