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 OverflowNumPy
numpy.org โบ devdocs โบ reference โบ generated โบ numpy.sort.html
numpy.sort โ NumPy v2.5.dev0 Manual
Return a sorted copy of an array.
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])
Videos
numpy argsort reverse
03:03
Efficiently sorting a numpy array in descending order? - YouTube
Efficiently sorting a numpy array in descending order?
Flatten, Flip, Sort Numpy Arrays - Numpy Tutorial for Beginners
04:01
Python NumPy|Sorting a Numpy Array | Python for Beginners | Learnerea ...
NumPy
numpy.org โบ doc โบ 2.2 โบ reference โบ generated โบ numpy.ndarray.sort.html
numpy.ndarray.sort โ NumPy v2.2 Manual
Return a sorted copy of an array.
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 - # Import numpy import numpy as np # Creating an empty numpy array arr = np.array([1, 8, 6, 9, 4]) # Display original array print("Orignal array:\n",arr,"\n") # 5 Sorted indices in ascending order res_asc = arr.argsort()[:5] # Display sorted indices in ascending order print("Sorted indices in ascending order:\n",res_asc,"\n") # 5 sorted indices i descending order res_asc = (-arr).argsort()[:5] # Display sorted indices in descending order print("Sorted indices in descending order:\n",res_asc)
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)
NumPy
numpy.org โบ doc โบ 2.3 โบ reference โบ generated โบ numpy.argsort.html
numpy.argsort โ NumPy v2.3 Manual
Perform an indirect sort along the given axis using the algorithm specified by the kind keyword.
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:
Codecademy
codecademy.com โบ article โบ sorting-and-unary-operations-in-num-py
Sorting and Unary Operations in NumPy | Codecademy
To sort in NumPy, we can use the numpy.sort() function. 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.
Programiz
programiz.com โบ python-programming โบ numpy โบ methods โบ sort
NumPy sort()
The sort() method returns a sorted array. import numpy as np array = np.array([10.20, 2.10, 9.9, -1.4])
DataCamp
datacamp.com โบ doc โบ numpy โบ sorting-arrays
NumPy Sorting Arrays
NumPy's array sorting capabilities are used to reorder elements within an array to facilitate data organization or analysis.
NumPy
numpy.org โบ doc โบ 2.1 โบ reference โบ generated โบ numpy.flip.html
numpy.flip โ NumPy v2.1 Manual
Reverse the order of elements in an array along the given axis.