Sort#

Alphabetical#

Order a table by the last_name column:

new_table = table.order_by('last_name')

Numerical#

Order a table by the cost column:

new_table = table.order_by('cost')

By date#

Order a table by the birth_date column:

new_table = table.order_by('birth_date')

Reverse order#

The order of any sort can be reversed by using the reverse keyword:

new_table = table.order_by('birth_date', reverse=True)

Multiple columns#

Because Python’s internal sorting works natively with sequences, we can implement multi-column sort by returning a tuple from the key function.

new_table = table.order_by(lambda row: (row['last_name'], row['first_name']))

This table will now be ordered by last_name, then first_name.

Random order#

import random

new_table = table.order_by(lambda row: random.random())