Yes. The sorted built-in accepts a key argument:

sorted(li,key=lambda x: x[1])
Out[31]: [['Jason', 1], ['John', 2], ['Jim', 9]]

note that sorted returns a new list. If you want to sort in-place, use the .sort method of your list (which also, conveniently, accepts a key argument).

or alternatively,

from operator import itemgetter
sorted(li,key=itemgetter(1))
Out[33]: [['Jason', 1], ['John', 2], ['Jim', 9]]

Read more on the python wiki.

Answer from roippi 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).
Discussions

How to sort a 2D array in descending or ascending order by a column?
Data.sort(key=lambda x: x[1], reverse=False) lambdas are kinda complicated to explain, but basically the key= argument specifies your own condition for sorting. x will be each element in your list, and x[1] is the same as Data[x][1], so it's saying sort by the second column. You can say x[0] if you want to sort by the string rather than the int, and change reverse to True/False for ascending/descending. More on reddit.com
🌐 r/learnpython
4
1
March 4, 2020
How to Sort Arrays by the nth Column in Python? - Data Exploration & Visualization - Data Science Dojo Discussions
I want to achieve this result using code by sorting the integers in a column, and having the rows sort themselves accordingly. Can anyone provide the code for this? More on discuss.datasciencedojo.com
🌐 discuss.datasciencedojo.com
2
0
February 28, 2023
How would you sort a 2D list by row based on the first column using a separate list as a key
If you want to sort it by month, why not use a dictionary of lists, such that dict['Nov'] = [0.0, 0.0, 0.09, etc.]? More on reddit.com
🌐 r/learnpython
3
1
November 22, 2022
Sorting 2d Array
Posted by u/PhantomWizard2099 - 2 votes and 14 comments More on reddit.com
🌐 r/learnpython
14
2
June 28, 2023
🌐
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'.
🌐
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 ...ding(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 ...
🌐
Data Science Dojo
discuss.datasciencedojo.com › data exploration & visualization
How to Sort Arrays by the nth Column in Python? - Data Exploration & Visualization - Data Science Dojo Discussions
February 28, 2023 - I want to achieve this result using code by sorting the integers in a column, and having the rows sort themselves accordingly. Can anyone provide the code for this?
Find elsewhere
🌐
Codefinity
codefinity.com › courses › v2 › 4f4826d5-e2f8-4ffd-9fd0-6f513353d70a › a4da9564-36a0-4109-b920-88fc7b89ddbb › e919a538-f041-447b-8082-e453a3c2aa5b
Learn Sorting 2D Arrays | Commonly used NumPy Functions
As you can see, simply passing our 2D array to the sort() function sorts each 1D array along the axis 1 (which is the default option in a 2D array). Setting axis=0 sorts each 1D array along the axis 0 (every column).
🌐
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 ...
🌐
Codemia
codemia.io › home › knowledge hub › sort a 2d array by a column value
Sort a 2d array by a column value | Codemia
December 28, 2024 - Sorting by a column is particularly useful when the column holds significant keys or identifiers, such as timestamps, prices, or unique IDs, which dictate the order in which records should be naturally organized. Several techniques can be used to sort a 2D array depending on the programming environment: Many modern programming languages like Python, Java, and JavaScript offer built-in functions or libraries that simplify the sorting of arrays based on specific row indices.
🌐
ProjectPro
projectpro.io › recipes › sort-array-by-nth-column
How to use NumPy to sort by column in an array- ProjectPro
October 19, 2023 - In this example, we're sorting the array by the second column (index 1). The sorted array will be stored in 'a'. ... This code efficiently sorts 'a' by its second column. You can adapt this code to sort by any other column of interest. For example, if you want to learn how to sort a NumPy 2D array by first column, use ‘0’ index instead of ‘1’ index.
🌐
Reddit
reddit.com › r/learnpython › how would you sort a 2d list by row based on the first column using a separate list as a key
r/learnpython on Reddit: How would you sort a 2D list by row based on the first column using a separate list as a key
November 22, 2022 -

so i have a data set of rain data over the course of a year where it ends up being formatted into something like this except its a lot longer

[['Nov', 0.0], ['Nov', 0.0], ['Nov', 0.09],['Feb', 0.0], ['Feb', 0.0], ['Feb', 0.09],['Oct', 0.56], ['Oct', 0.0], ['Oct', 0.03], ['Oct', 0.62]]

now I figured I would need another list to function as a lookup to help sort this data by month according to calendar order so I made one

month_lookup = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

but now I am a tad confused about how I would go about using something like the sorted() function to use the lookup as the way to sort the 2D list above

🌐
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,...
🌐
GeeksforGeeks
geeksforgeeks.org › python › sorting-list-of-lists-with-first-element-of-each-sub-list-in-python
Python Sorting List Of Lists By The First Element Of Each ...
July 23, 2025 - In this approach, we are using the sort() method with a Custom Comparator as the key to sort the list in-place based on the first element of each sub-list.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-reverse-sort-row-matrix-integration
Python | Reverse Sort Row Matrix integration - GeeksforGeeks
April 28, 2025 - Create a 2D numpy array using the two lists provided and transpose it to create a row matrix. Use numpy.argsort() method to get the indices that would sort the array in ascending order based on the first column.
🌐
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]]
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › row-wise-sorting-2d-array
Row wise sorting a 2D array - GeeksforGeeks
April 3, 2025 - Each row is sorted independently without changing the order of elements in other rows. The std::sort() function can be used to sort the elements of each row efficiently. ... The idea is to traverse the matrix row by row and apply std::sort() ...
🌐
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.
🌐
Codehamster
codehamster.com › codehamster | "everything should be made as simple as possible, but not simpler." -- albert einstein › codes › python › sort 2d array in python
sort 2d array in python
October 12, 2024 - There are many ways to sort a 2d arrays. In general, you can use the sorted(), np.array.sort(),... But you should also understand what is meaning of list1.sort(list2) ravel() vs. flatten() what return the key function back in sorted() what will be returned by np.lexsort(), np.argsort()