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
๐ŸŒ
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ numpy โ€บ how-to-use-numpy-argsort-in-descending-order-in-python
How to use numpy.argsort in Descending order in Python - GeeksforGeeks
July 23, 2025 - In this example, we are negating the NumPy Array to sort it in descending order using numy.argsort(). Here, we will use argsort() to get the indices that would sort the original array in ascending order.
๐ŸŒ
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.
๐ŸŒ
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 - Using [::-1] reverses those indices to achieve descending order. ... As you can see, the array is now sorted based on the first column, but in descending order.
Find elsewhere
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ numpy sort array
NumPy Sort Array - Spark By {Examples}
March 27, 2024 - You can sort a NumPy array in descending order. You can achieve this by using the numpy.sort() function to get a sorted copy and then reversing the order of the elements.
๐ŸŒ
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 ...
๐ŸŒ
Sarthaks eConnect
sarthaks.com โ€บ 3543908 โ€บ how-can-i-sort-a-numpy-array-in-descending-order
How can I sort a NumPy array in descending order? - Sarthaks eConnect | Largest Online Education Community
August 12, 2023 - LIVE Course for free ยท To sort the array in descending order, you can reverse the sorted array obtained from np.sort() using slicing
๐ŸŒ
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.
๐ŸŒ
Wellsr
wellsr.com โ€บ python โ€บ sorting-numpy-arrays-in-python
How to Sort NumPy Arrays in Python (with Examples) - wellsr.com
April 29, 2022 - One way to sort a NumPy array in descending order is to first sort the array in ascending order using the sort() function and then you can apply any array reversal technique to get the final array where the items are sorted in descending order.
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ python-program-to-sort-array-in-descending-order
Python Program to Sort Array in Descending Order
April 3, 2025 - Write a Python Program to sort Numpy Array items in Descending order. First, the Numpy sort function (orarr.sort()) sorts the array items in ascending 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])....
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ third-party โ€บ numpy โ€บ sort
Python Numpy sort() - Sort Elements | Vultr Docs
December 31, 2024 - In this article, you will learn how to proficiently use the sort() function to manage array data. You'll explore how to apply it to various array structures and discover tips for optimal sorting strategies. Import the NumPy library.
๐ŸŒ
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.
๐ŸŒ
Data Science Parichay
datascienceparichay.com โ€บ home โ€บ blog โ€บ sort numpy array in descending order
Sort Numpy Array in Descending Order - Data Science Parichay
July 2, 2022 - To sort a Numpy array in descending order, first, sort it in ascending order and then reverse the array using slicing.