File: sort.rst

package info (click to toggle)
python-agate 1.13.0-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,008 kB
  • sloc: python: 8,578; makefile: 126
file content (61 lines) | stat: -rw-r--r-- 1,129 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
====
Sort
====

Alphabetical
============

Order a table by the :code:`last_name` column:

.. code-block:: python

    new_table = table.order_by('last_name')

Numerical
=========

Order a table by the :code:`cost` column:

.. code-block:: python

    new_table = table.order_by('cost')

.. _sort_by_date:

By date
=======

Order a table by the :code:`birth_date` column:

.. code-block:: python

    new_table = table.order_by('birth_date')

Reverse order
=============

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

.. code-block:: python

    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.

.. code-block:: python

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

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

Random order
============

.. code-block:: python

    import random

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