How to sort a NumPy array

Array order affects how rankings, thresholds, and comparisons appear in a NumPy result. Before comparing values across a matrix, decide whether each row, each column, or the whole array should be ordered.

The np.sort() function returns a sorted copy and leaves the input array unchanged. The array method sort() can sort in place, but a copy-returning sort keeps the original values visible while checking the axis behavior.

When another array carries labels, timestamps, or IDs for the same values, sort indices instead of sorting the values alone. np.argsort() returns the order, and applying that order keeps parallel arrays matched to the sorted data.

Steps to sort a NumPy array:

  1. Create a script that sorts scores by row, by column, as one flattened array, and with stable index order.
    array-sort.py
    import numpy as np
     
    scores = np.array(
        [
            [88, 70, 95],
            [62, 91, 77],
        ]
    )
    labels = np.array(["west", "east", "north", "south"])
    totals = np.array([92, 88, 92, 75])
     
    row_sorted = np.sort(scores, axis=1)
    column_sorted = np.sort(scores, axis=0)
    flat_sorted = np.sort(scores, axis=None)
    rank_order = np.argsort(totals, kind="stable")
     
    print("scores:")
    print(scores)
    print("row sorted:")
    print(row_sorted)
    print("column sorted:")
    print(column_sorted)
    print("flat sorted:", flat_sorted)
    print("rank order:", rank_order)
    print("labels by total:", labels[rank_order])
    print("totals sorted:", totals[rank_order])
     
    assert row_sorted.tolist() == [[70, 88, 95], [62, 77, 91]]
    assert column_sorted.tolist() == [[62, 70, 77], [88, 91, 95]]
    assert flat_sorted.tolist() == [62, 70, 77, 88, 91, 95]
    assert labels[rank_order].tolist() == ["south", "east", "west", "north"]
    assert totals[rank_order].tolist() == [75, 88, 92, 92]
     
    print("sorting checks passed")

    np.sort() returns sorted copies for row_sorted, column_sorted, and flat_sorted. Use an in-place method such as scores.sort(axis=1) only when the original order no longer needs to be inspected.

  2. Run the script.
    $ python3 array-sort.py
    scores:
    [[88 70 95]
     [62 91 77]]
    row sorted:
    [[70 88 95]
     [62 77 91]]
    column sorted:
    [[62 70 77]
     [88 91 95]]
    flat sorted: [62 70 77 88 91 95]
    rank order: [3 1 0 2]
    labels by total: ['south' 'east' 'west' 'north']
    totals sorted: [75 88 92 92]
    sorting checks passed
  3. Check the row, column, and flat output sections.

    axis=1 sorts within each row. axis=0 sorts down each column. axis=None flattens the input first and returns one sorted one-dimensional array.

  4. Check the label order for the totals.

    np.argsort(totals, kind=“stable”) returns positions [3 1 0 2]. The stable sort keeps west before north because both totals are 92 and west appears first in labels.

  5. Confirm that the final line shows sorting checks passed.

    The assertions prove that the row sort, column sort, flattened sort, stable label order, and sorted totals matched the expected arrays.