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)
๐ŸŒ
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 ascending order based on the values in the second column: #define new matrix with rows sorted in ascending order by values in second column x_sorted_asc = x[x[:, 1].argsort()] #view sorted matrix print(x_sorted_asc) [[10 5 7] [11 9 2] [14 12 8]]
๐ŸŒ
ProjectPro
projectpro.io โ€บ recipes โ€บ sort-array-by-nth-column
How to use NumPy to sort by column in an array- ProjectPro
October 19, 2023 - Whether you need to sort by the first column, second column, or any other column, NumPy provides a straightforward method for achieving this. Let's explore how to use NumPy to sort by a specific column efficiently. Learn how to build Regression (Linear,Ridge,Lasso) Models in NumPy Python ยท Learn how to sort a numpy array by a column with these easy-to-follow steps
๐ŸŒ
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))
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ numpy โ€บ numpy_array_sort.asp
NumPy Sorting Arrays
The NumPy ndarray object has a function called sort(), that will sort a specified array.
Find elsewhere
๐ŸŒ
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 - # Importing the NumPy library import numpy as np # Displaying a message indicating the original array is going to be printed print("Original array:\n") # Generating a 3x3 array of random integers from 0 to 10 nums = np.random.randint(0, 10, (3, 3)) # Displaying the original array print(nums) # Displaying a message indicating sorting the array by the nth column print("\nSort the said array by the nth column: ") # Sorting the array by the values in the nth column using argsort print(nums[nums[:, 1].argsort()])
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ generated โ€บ numpy.sort.html
numpy.sort โ€” NumPy v2.1 Manual
Use the order keyword to specify a field to use when sorting a structured array: >>> dtype = [('name', 'S10'), ('height', float), ('age', int)] >>> values = [('Arthur', 1.8, 41), ('Lancelot', 1.9, 38), ...
๐ŸŒ
Like Geeks
likegeeks.com โ€บ home โ€บ python โ€บ numpy โ€บ sorting numpy arrays: a comprehensive guide
Sorting NumPy Arrays: A Comprehensive Guide
In this example, we have a NumPy ... one will come first. To sort a 2D (or higher) array by multiple columns, you can use lexsort() as weโ€™ll see later in this tutorial....
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.sort.html
numpy.sort โ€” NumPy v2.5 Manual
Use the order keyword to specify a field to use when sorting a structured array: >>> dtype = [('name', 'S10'), ('height', float), ('age', int)] >>> values = [('Arthur', 1.8, 41), ('Lancelot', 1.9, 38), ...
๐ŸŒ
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. In this if we want to sort 2D numpy array by 2nd column then we have to change the positions of all the rows ...
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.ndarray.sort.html
numpy.ndarray.sort โ€” NumPy v2.5 Manual
>>> import numpy as np >>> a = np.array([[1,4], [3,1]]) >>> a.sort(axis=1) >>> a array([[1, 4], [1, 3]]) >>> a.sort(axis=0) >>> a array([[1, 3], [1, 4]])
๐ŸŒ
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.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ numpy โ€บ methods โ€บ sort
NumPy sort()
# sort a string array based on their ASCII values. array2 = np.sort(array) print(array2) Output ยท ['Apple' 'Ball' 'Cat' 'apple'] Multidimensional arrays are sorted based on the given axis. import numpy as np array = np.array([[3, 10, 2], [1, 5, 7], [2, 7, 5]]) # sort column wise based on the axis 1 array2 = np.sort(array) # flattens the given array and sorts the flattened array array3 = np.sort(array, axis = None) # sort array row wise array4 = np.sort(array, axis = 0) print('Sorted based on last axis(1): \n', array2) print('Sorted a flattened array', array3) print('Sorted based on axis 0: \n', array4) Output ยท
๐ŸŒ
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 - This will result in the elements of the second column being arranged in ascending order while maintaining the corresponding elements in the other columns. Some examples of sorting by column in NumPy arrays include sorting a dataset by a specific attribute, organizing data in a table, or arranging values in a matrix.
๐ŸŒ
Python Data Science Handbook
jakevdp.github.io โ€บ PythonDataScienceHandbook โ€บ 02.08-sorting.html
Sorting Arrays | Python Data Science Handbook
The first element of this result gives the index of the smallest element, the second value gives the index of the second smallest, and so on. These indices can then be used (via fancy indexing) to construct the sorted array if desired: ... A useful feature of NumPy's sorting algorithms is the ability to sort along specific rows or columns of a multidimensional array using the axis argument.
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python:numpy โ€บ built-in functions โ€บ .sort()
Python:NumPy | Built-in Functions | .sort() | Codecademy
July 25, 2025 - Yes, use the axis parameter. numpy.sort(array, axis=0) sorts each column independently, while numpy.sort(array, axis=1) sorts each row independently.