To sort by the second column of a:

a[a[:, 1].argsort()]
Answer from Steve Tjoa on Stack Overflow
🌐
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 - By now, you should feel comfortable sorting arrays based on any column, whether it’s ascending, descending, or using a specific column. The real beauty of argsort() lies in its flexibility, which is why it's one of the most powerful tools for sorting data. ... You might be wondering: “Can I sort more than one column at once?” The answer is yes! You can use np.lexsort() to perform multi-key sorting. Here's how: import numpy as np arr = np.array([[3, 7], [2, 5], [4, 6]]) # Sorting by the second column, then the first column sorted_arr = arr[np.lexsort((arr[:, 0], arr[:, 1]))] print(sorted_arr)
🌐
ProjectPro
projectpro.io › recipes › sort-array-by-nth-column
How to use NumPy to sort by column in an array- ProjectPro
October 19, 2023 - This 'a' array serves as our data to be sorted by one of its columns. To sort the NumPy array by a particular column, use NumPy's argsort() function. In this example, we're sorting the array by the second column (index 1).
🌐
Statology
statology.org › home › how to sort a numpy array by column (with examples)
How to Sort a NumPy Array by Column (With Examples)
December 6, 2021 - We can use the following code to sort the rows of the NumPy array in descending order based on the values in the second column: #define new matrix with rows sorted in descending order by values in second column x_sorted_desc = x[x[:, 1].argsort()[::-1]] #view sorted matrix print(x_sorted_desc) [[14 12 8] [11 9 2] [10 5 7]]
🌐
NumPy
numpy.org › doc › 1.25 › reference › generated › numpy.lexsort.html
numpy.lexsort — NumPy v1.25 Manual
Given multiple sorting keys, which can be interpreted as columns in a spreadsheet, lexsort returns an array of integer indices that describes the sort order by multiple columns. The last key in the sequence is used for the primary sort order, the second-to-last key for the secondary sort order, and so on.
🌐
OpenSourceOptions
opensourceoptions.com › sort-numpy-arrays-by-columns-or-rows
Sort NumPy Arrays By Columns or Rows – OpenSourceOptions
NumPy arrays can be sorted by a single column, row, or by multiple columns or rows using the argsort() function. The argsort function returns a list of indices that will sort the values in an array in ascending value. The kind argument of the argsort function makes it possible to sort arrays ...
🌐
Arab Psychology
scales.arabpsychology.com › home › stats › how to easily sort a numpy array by column
How To Easily Sort A NumPy Array By Column
December 2, 2025 - The key to sorting a NumPy array by column lies not in sorting the values directly, but in determining the correct order of indices. This is precisely the function of np.argsort(). This function returns an array of indices that, when used to index the original array, produces a sorted result.
🌐
w3resource
w3resource.com › python-exercises › numpy › basic › numpy-basic-exercise-52.php
NumPy: Sort a given array by row and column in ascending order - w3resource
August 28, 2025 - By applying sorting operations separately to rows and columns, the program efficiently arranges the elements of the array in ascending order along both dimensions, facilitating data organization and analysis. ... # Importing the NumPy library with an alias 'np' import numpy as np # Creating a NumPy array 'nums' containing values in a 3x3 matrix nums = np.array([[5.54, 3.38, 7.99], [3.54, 4.38, 6.99], [1.54, 2.39, 9.29]]) # Printing a message indicating the original array print("Original array:") print(nums) # Sorting the array 'nums' by rows in ascending order and displaying the sorted result print("\nSort the said array by row in ascending order:") print(np.sort(nums)) # Sorting the array 'nums' by columns in ascending order and displaying the sorted result print("\nSort the said array by column in ascending order:") print(np.sort(nums, axis=0))
Find elsewhere
🌐
IncludeHelp
includehelp.com › python › how-to-sort-numpy-arrays-by-column.aspx
Python - How to Sort NumPy Arrays by Column?
December 28, 2023 - To sort NumPy arrays by column in Python, we use numpy.argsort() where we will pass the sliced array part i.e, the first column of the array but this will only return the sorted indices and hence we need to pass this result in the indexing of the original array.
🌐
w3resource
w3resource.com › python-exercises › numpy › python-numpy-sorting-and-searching-exercise-9.php
NumPy: Sort an given array by the nth column - w3resource
August 30, 2025 - Create a function that takes a 2D array and a column index as input, then returns the array sorted by that column.
🌐
Medium
medium.com › data-science › sort-numpy-arrays-by-columns-or-rows-18dfd2ef28c4
Sort NumPy Arrays By Columns or Rows | by Konrad Hafen | TDS Archive | Medium
March 30, 2021 - NumPy arrays can be sorted by a single column, row, or by multiple columns or rows using the argsort() function. The argsort function returns a list of indices that will sort the values in an array in ascending value.
🌐
YouTube
youtube.com › watch
Sort Numpy Arrays by Row and Column with numpy.argsort - YouTube
Use numpy.argsort to sort numpy arrays by columns and rows. This tutorial will give you step-by-step instructions to sort numpy arrays with python.Website: h...
Published: June 29, 2021
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.sort.html
numpy.sort — NumPy v2.5 Manual
Sorting algorithm. The default is ‘quicksort’. Note that both ‘stable’ and ‘mergesort’ use timsort or radix sort under the covers and, in general, the actual implementation will vary with data type.
🌐
TutorialsPoint
tutorialspoint.com › article › write-a-python-code-to-sort-an-array-in-numpy-by-the-nth-column
Write a Python code to sort an array in NumPy by the nth column?
October 20, 2022 - Enter n value and create a variable to store it. Use the argsort() function, to sort the input array by the nth column and print the resultant sorted array in ascending order of the 1st column.
🌐
sqlpey
sqlpey.com › python › efficient-numpy-array-sorting-by-specific-columns
Efficient NumPy Array Sorting by Specific Columns: Techniques Compared
November 4, 2025 - The most idiomatic and often fastest way to sort a NumPy array based on the $N^{th}$ column is by using the argsort() method on that column to derive the correct index order, and then applying that index order back to the array.
🌐
ProjectPro
projectpro.io › recipes › sort-2d-array-by-column-numpy
How to sort a 2D array by a column in numpy? -
June 22, 2022 - How to sort a 2D array by a column. yes we can do this and for that we have to use the "sort" function available in the numpy library.
🌐
pythontutorials
pythontutorials.net › blog › numpy-sort-by-column
Numpy Sort by Column: A Comprehensive Guide — pythontutorials.net
For instance, if you have a 2 - ... will re - order the students according to their scores in that subject. The numpy.sort() function can be used to sort an array....
🌐
Arab Psychology
scales.arabpsychology.com › home › how do i sort a numpy array by column? can you provide some examples?
How Do I Sort A NumPy Array By Column? Can You Provide Some Examples?
July 2, 2024 - Sorting a NumPy array by column refers to arranging the elements of the array in ascending or descending order based on a specific column. This is achieved by using the “np.sort” function and specifying the axis as the desired column.
🌐
Like Geeks
likegeeks.com › home › python › numpy › sorting numpy arrays: a comprehensive guide
Sorting NumPy Arrays: A Comprehensive Guide
To sort a NumPy structured array by multiple columns, you can pass the column names you want to sort to the order parameter of the numpy.sort() function:
🌐
Codecademy
codecademy.com › article › sorting-and-unary-operations-in-num-py
Sorting and Unary Operations in NumPy | Codecademy
The numpy.sort() function allows ... as follows: ... axis: Determines the axis along which the sorting will occur. Setting axis=0 sorts the columns ......