1. Python already sorts numbers numerically. If you want lexigraphic sort, you'd need to convert numbers to strings.

  2. Easiest way to sort by multiple columns is by employing the fact that Python guarantees sorts are stable - which means order of elements is kept in case they are tied in regards to sorting key. Using that, you can sort from least significant to most significant key, resulting in expected output:

res = sorted(
        sorted(
            sorted(
                sorted(arr, key = lambda x: x[3]),
                key = lambda x: x[2]),
            key = lambda x: x[1]),
        key = lambda x: x[0])

Output:

[96, 132, 122, 112]
[96, 132, 329, 114]
[130, 175, 75, 152]
[130, 175, 182, 152]
[174, 218, 141, 196]
[174, 218, 328, 194]
[661, 701, 21, 683]
[707, 746, 375, 724]
[768, 806, 26, 788]
[768, 806, 286, 787]
[894, 933, 80, 914]
[894, 933, 166, 913]
[957, 998, 305, 980]
[957, 998, 394, 974]
Answer from matszwecja on Stack Overflow
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Sort a 2D List in Python | note.nkmk.me
July 27, 2023 - source: list_2d_sort.py ยท By default, the np.sort() function sorts each row. If you set axis=0, it will sort each column instead. The function returns a NumPy array (ndarray).
Top answer
1 of 2
2
  1. Python already sorts numbers numerically. If you want lexigraphic sort, you'd need to convert numbers to strings.

  2. Easiest way to sort by multiple columns is by employing the fact that Python guarantees sorts are stable - which means order of elements is kept in case they are tied in regards to sorting key. Using that, you can sort from least significant to most significant key, resulting in expected output:

res = sorted(
        sorted(
            sorted(
                sorted(arr, key = lambda x: x[3]),
                key = lambda x: x[2]),
            key = lambda x: x[1]),
        key = lambda x: x[0])

Output:

[96, 132, 122, 112]
[96, 132, 329, 114]
[130, 175, 75, 152]
[130, 175, 182, 152]
[174, 218, 141, 196]
[174, 218, 328, 194]
[661, 701, 21, 683]
[707, 746, 375, 724]
[768, 806, 26, 788]
[768, 806, 286, 787]
[894, 933, 80, 914]
[894, 933, 166, 913]
[957, 998, 305, 980]
[957, 998, 394, 974]
2 of 2
-1

Using argsort :

arr = np.array([[130, 175, 75, 152],
 [96, 132, 122, 112],
 [174, 218, 141, 196],
 [661, 701, 21, 683],
 [707, 746, 375, 724],
 [957, 998, 305, 980],
 [768, 806, 26, 788],
 [957, 998, 394, 974],
 [768, 806, 286, 787],
 [174, 218, 328, 194],
 [894, 933, 80, 914],
 [130, 175, 182, 152],
 [96, 132, 329, 114],
 [894, 933, 166, 913]])

sorted_arr = arr[arr[:, 0].argsort()]
print(sorted_arr)

outputs what you want. Short explanation (more details here) is getting the sorted indices of the columns of your array using argsort() then use the argsort result as the row indices and assign the resulting array back to a, as follows.

If you want (as in your second point ?) sort it based on the second column, use sorted_arr = arr[arr[:, 1].argsort()] or with a set priority in your columns, look in the mentioned link you should find what you want.

Discussions

python - How to sort multidimensional array by column? - Stack Overflow
1 How to sort a multidimensional list to alphabetical order based on its first row letters in python ... 3 how to sort a multidimensional array with one column in ascending order and another column in descending order? More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Sorting a 2D numpy array by multiple axes - Stack Overflow
The title says "sorting 2D arrays". Although the questioner uses an (N,2)-shaped array, it's possible to generalize unutbu's solution to work with any (N,M) array, as that's what people might actually be looking for. One could transpose the array and use slice notation with negative step to pass all the columns ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Sorting 2d Array
Posted by u/PhantomWizard2099 - 2 votes and 14 comments More on reddit.com
๐ŸŒ r/learnpython
14
2
June 28, 2023
python - Sort numpy 2d array by multiple columns - Stack Overflow
Communities for your favorite technologies. Explore all Collectives ยท Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
๐ŸŒ stackoverflow.com
February 15, 2021
๐ŸŒ
Like Geeks
likegeeks.com โ€บ home โ€บ python โ€บ numpy โ€บ sorting numpy arrays: a comprehensive guide
Sorting NumPy Arrays: A Comprehensive Guide
To sort a 2D (or higher) array by multiple columns, you can use lexsort() as weโ€™ll see later in this tutorial.
๐ŸŒ
thisPointer
thispointer.com โ€บ home โ€บ numpy โ€บ sorting 2d numpy array by column or row in python
Sorting 2D Numpy Array by column or row in Python - thisPointer
July 5, 2019 - For this we need to change positioning of all rows in 2D numpy array based on sorted values of 2nd column i.e. column at index 1. Letโ€™s see how to do that, How to save Numpy Array to a CSV File using numpy.savetxt() in Python
๐ŸŒ
Script Everything
scripteverything.com โ€บ posts โ€บ how to sort 2d array in python: 1 line of code without numpy
How To Sort 2D Array In Python: 1 Line Of Code Without Numpy | Script Everything
July 29, 2021 - To sort a two-dimensional list using multiple columns and different sorting methods (eg. descending order for one, ascending order for another) in Python, without using any imported libraries, use the built-in sort() list method and sorted() function. By using the built-in sort list method ...
๐ŸŒ
Medium
medium.com โ€บ @whyamit404 โ€บ sorting-numpy-arrays-by-column-using-numpy-sort-c45a44660dcc
Sorting NumPy Arrays by Column Using numpy.sort() | by whyamit404 | Medium
February 26, 2025 - Hereโ€™s how you can use it: ... For column-wise sorting, you use axis=0. This means each column will be sorted independently. kind (optional): Lets you choose the sorting algorithm, like 'quicksort' or 'mergesort'.
Find elsewhere
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ sort 2d array python
How to Sort 2D Array in Python | Delft Stack
February 2, 2024 - from operator import itemgetter lst = [ ("john", "C", 15), ("jane", "A", 12), ("dave", "D", 10), ] sorted_lst = sorted(lst, key=itemgetter(1)) print(sorted_lst) ... Enjoying our tutorials?
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ python-program-to-sort-the-2d-array-across-columns
Python Program to Sort the 2D Array Across Columns
March 27, 2026 - import numpy as np # Original 2D array arr = [[7, 9, 5, 7], [9, 5, 9, 4], [2, 7, 8, 6], [8, 6, 6, 5]] print("Original array:") for row in arr: print(row) Original array: [7, 9, 5, 7] [9, 5, 9, 4] [2, 7, 8, 6] [8, 6, 6, 5] ... def sort_columns_descending(matrix): """Sort each column in descending order using bubble sort""" rows = len(matrix) cols = len(matrix[0]) # Sort each column independently for col in range(cols): # Bubble sort for current column for i in range(rows - 1): for j in range(rows - 1 - i): if matrix[j][col] < matrix[j + 1][col]: # Swap elements in the column matrix[j][col], mat
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.lexsort.html
numpy.lexsort โ€” NumPy v2.5 Manual
To sort lexicographically with argsort, we would need to provide a structured array. >>> x = np.array([(ai, bi) for ai, bi in zip(a, b)], ... dtype = np.dtype([('x', int), ('y', int)])) >>> np.argsort(x) # or np.argsort(x, order=('x', 'y')) array([2, 0, 4, 6, 5, 3, 1]) The zeroth axis of keys always corresponds with the sequence of keys, so 2D arrays are treated just like other sequences of keys.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ sorting 2d array
r/learnpython on Reddit: Sorting 2d Array
June 28, 2023 - Step 1. Go through the 2d array. Place each entry into a single array. Step 2. Sort the single array. Step 3. Put the results of the sorted array into a new array, grouping them by two entries.
๐ŸŒ
ProjectPro
projectpro.io โ€บ recipes โ€บ sort-2d-array-by-column-numpy
How to sort a 2D array by a column in numpy? -
June 22, 2022 - This is Sample 2D array : [[100 101 500 104] [201 202 203 204] [301 300 600 307]] Index = 2 Array_sort = Sample_array[Sample_array[:,Index].argsort()] print("The original array is:","\n","\n", Sample_array, "\n") print("The sorted array is:", "\n", "\n", Array_sort) The original array is: [[100 101 500 104] [201 202 203 204] [301 300 600 307]] The sorted array is: [[201 202 203 204] [100 101 500 104] [301 300 600 307]]
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-21310.html
sorting 2D lists by column
September 24, 2019 - Hello everyone, I would like to sort a multidimensional list by the second column. Example: x = [ [1,18,2], [2,9,4], [3,1,1] ] x = sorted(x, key=lambda x: x[2]) print(x)The output I am looking for should be: [ [3,1,1], [2,9,4], [1,...
๐ŸŒ
DaniWeb
daniweb.com โ€บ programming โ€บ software-development โ€บ threads โ€บ 288688 โ€บ sorting-multidimensional-arrays
python - Sorting Multidimensional Arrays [SOLVED] | DaniWeb
June 8, 2010 - The only time I have had anything work is when I use x.sort(0) in which it sorted all information from lowest to highest in all columns. Anything else has left the array exactly the same as how it was entered. ... By default sort() sorts by the first item in each sublist ...
๐ŸŒ
Finxter
blog.finxter.com โ€บ 5-best-ways-to-sort-a-2d-array-across-columns-in-python
5 Best Ways to Sort a 2D Array Across Columns in Python โ€“ Be on the Right Side of Change
This method involves using the built-in sort() method in Python. A custom key function is defined to perform the sorting across columns by first transposing the array, sorting, and then transposing it back. ... def sort_2d_array_columns(arr): transposed = list(map(list, zip(*arr))) for row ...
๐ŸŒ
Codecademy Forums
discuss.codecademy.com โ€บ data science
Is there a way to sort a two-dimensional list on the values of the second 'column'? - Data Science - Codecademy Forums
September 25, 2023 - For example, in the Python Lists Project, I have created a two-dimensional list by joining the names and insurance costs, in that order. Is it possible to sort the list by the insurance cost? Thanks ๐Ÿ™‚ link: https://www.codecademy.com/journeys/data-scientist-ml/paths/dsmlcj-22-data-scien...