An easy way to do this is to use the sort() or sorted() functions.
lines = shopping.readlines() lines.sort()
As an alternative:
lines = sorted(shopping.readlines())
The disadvantage is that you have to read the entire file in memory. If this is not a problem, you can use this simple code.
source share