temp[::-1].sort() sorts the array in place, whereas np.sort(temp)[::-1] creates a new array.
In [25]: temp = np.random.randint(1,10, 10)
In [26]: temp
Out[26]: array([5, 2, 7, 4, 4, 2, 8, 6, 4, 4])
In [27]: id(temp)
Out[27]: 139962713524944
In [28]: temp[::-1].sort()
In [29]: temp
Out[29]: array([8, 7, 6, 5, 4, 4, 4, 4, 2, 2])
In [30]: id(temp)
Out[30]: 139962713524944
Answer from Padraic Cunningham on Stack Overflow Top answer 1 of 12
231
temp[::-1].sort() sorts the array in place, whereas np.sort(temp)[::-1] creates a new array.
In [25]: temp = np.random.randint(1,10, 10)
In [26]: temp
Out[26]: array([5, 2, 7, 4, 4, 2, 8, 6, 4, 4])
In [27]: id(temp)
Out[27]: 139962713524944
In [28]: temp[::-1].sort()
In [29]: temp
Out[29]: array([8, 7, 6, 5, 4, 4, 4, 4, 2, 2])
In [30]: id(temp)
Out[30]: 139962713524944
2 of 12
186
>>> a=np.array([5, 2, 7, 4, 4, 2, 8, 6, 4, 4])
>>> np.sort(a)
array([2, 2, 4, 4, 4, 4, 5, 6, 7, 8])
>>> -np.sort(-a)
array([8, 7, 6, 5, 4, 4, 4, 4, 2, 2])
NumPy
numpy.org โบ devdocs โบ reference โบ generated โบ numpy.sort.html
numpy.sort โ NumPy v2.5.dev0 Manual
In numpy versions >= 1.4.0 nan values are sorted to the end.
Videos
04:15
Efficiently sorting a numpy array in descending order? - YouTube
Efficiently sorting a numpy array in descending order?
01:48
How to Rank Elements of a NumPy Array in Descending Order - YouTube
03:05
Python: How to Sort Arrays In Ascending and Descending Order (String ...
04:01
Python NumPy|Sorting a Numpy Array | Python for Beginners | Learnerea ...
Reddit
reddit.com โบ r/learnpython โบ problem with sorting in descending order
r/learnpython on Reddit: Problem with sorting in descending order
October 6, 2023 -
Hi, I am trying to sort in descending order in jupyter notebook
I tried from:StackExchange
import numpy as np #import numpy
games = np.array(['FIFA 2020','Red Dead Redemption','Fallout','GTA','NBA 2018','Need For Speed'])
print(games)
print("1",np.sort(games))
games = np.sort(games)
np.sort(games, order='descending')But I am getting the error
ValueError: Cannot specify order when the array has no fields.
Somebody, please guide me.
Zulfi.
Top answer 1 of 2
3
I think you are misunderstanding the order parameter. It doesn't do what you seem to think it does: >>> import numpy as np >>> help(np.sort) Help on _ArrayFunctionDispatcher in module numpy: sort(a, axis=-1, kind=None, order=None) Return a sorted copy of an array. Parameters ---------- ... order : str or list of str, optional When `a` is an array with fields defined, this argument specifies which fields to compare first, second, etc. A single field can be specified as a string, and not all fields need be specified, but unspecified fields will still be used, in the order in which they come up in the dtype, to break ties. So order requires your array to have pre-defined "fields", whose members you pass in as order in the order you'd like them sorted according to. "descending" is obviously not a "field" of your array, hence it's not working. Note that despite ascending-order sorting seeming to work in your sample code, if you specify the order as such, you get the same error: >>> import numpy as np >>> games = np.array(['FIFA 2020','Red Dead Redemption','Fallout','GTA','NBA 2018','Need For Speed']) >>> np.sort(games, order='ascending') Traceback (most recent call last): ... ValueError: Cannot specify order when the array has no fields. Looking into this, it seems numpy.sort actually doesn't support descending order. So to achieve the same, just sort in ascending order, then slice in reverse: >>> np.sort(games)[::-1] array(['Red Dead Redemption', 'Need For Speed', 'NBA 2018', 'GTA', 'Fallout', 'FIFA 2020'], dtype='
2 of 2
3
Do you need numpy here? Seems overkill. games = ['FIFA 2020','Red Dead Redemption','Fallout','GTA','NBA 2018','Need For Speed'] sorted(games, reverse=True)
Codecademy
codecademy.com โบ article โบ sorting-and-unary-operations-in-num-py
Sorting and Unary Operations in NumPy | Codecademy
By default, it sorts an array of numbers in ascending order. While direct sorting in descending order isnโt available through this function, it can be achieved by sorting in ascending order and then reversing the result.
W3Schools
w3schools.com โบ python โบ numpy โบ numpy_array_sort.asp
NumPy Sorting Arrays
NumPy Editor NumPy Quiz NumPy Exercises NumPy Syllabus NumPy Study Plan NumPy Certificate ... Sorting means putting elements in an ordered sequence. Ordered sequence is any sequence that has an order corresponding to elements, like numeric or alphabetical, ascending or descending.
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 - Notice that the rows are now sorted in descending order (largest to smallest) based on the values in the second column. The following tutorials explain how to perform other common operations in Python: How to Find Index of Value in NumPy Array How to Get Specific Column from NumPy Array How ...
NumPy
numpy.org โบ doc โบ 2.1 โบ reference โบ generated โบ numpy.argsort.html
numpy.argsort โ NumPy v2.1 Manual
It returns an array of indices of the same shape as a that index data along the given axis in sorted order.
DataCamp
datacamp.com โบ doc โบ numpy โบ sorting-arrays
NumPy Sorting Arrays
In this syntax, a is the array to be sorted, axis specifies the axis along which to sort, kind determines the sorting algorithm, and order is used when sorting structured arrays. Structured arrays are arrays with fields, similar to columns in a database table. import numpy as np arr = np.array([3, 1, 2]) sorted_arr = np.sort(arr)
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
12345678 import numpy as np array_2d ... in descending order along a given axis, you need to use two slices: one full slice ([:]) and another with a negative step ([::-1])....
Codecademy
codecademy.com โบ docs โบ python:numpy โบ ndarray โบ .sort()
Python:NumPy | ndarray | .sort() | Codecademy
October 30, 2025 - In the following codebyte example, a NumPy array nf is created with integers, sorted in ascending order using NumPyโs .sort() method, and then reversed to display the values in descending order:
NumPy
numpy.org โบ doc โบ 2.2 โบ reference โบ generated โบ numpy.argsort.html
numpy.argsort โ NumPy v2.2 Manual
It returns an array of indices of the same shape as a that index data along the given axis in sorted order.
IncludeHelp
includehelp.com โบ python โบ is-it-possible-to-use-numpy-argsort-in-descending-order.aspx
Python - Is it possible to use numpy.argsort() in descending order?
May 25, 2023 - To use the numpy.argsort() method in descending order in Python, if we negate an array, the lowest elements become the highest elements and the highest elements become the lowest elements.