my_array.sort sorts the list "in place":
Instead of returning a sorted list, as you seem to expect, my_array.sort() sorts my_array "in place" and doesn't return anything. Try this
my_array = [5, 2, 8, 1, 9]
my_array.sort()
print(my_array) # [1, 2, 5, 8, 9]
If you want it sorted in descending order use this:
my_array = [5, 2, 8, 1, 9]
my_array.sort()
my_array.reverse()
print(my_array) # [9, 8, 5, 2, 1]
Answer from TheTridentGuy supports Ukraine on Stack Overflow10:06
How to Order Lists in Python (Visually Explained) | sort(), sorted(), ...
17:13
Sort an Array - Leetcode 912 - Python - YouTube
04:55
Sort List in Python | Ascending & Descending | CodingFacts - YouTube
05:54
How to sort a list in ascending and descending order in Python? ...
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
185
>>> 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])
GeeksforGeeks
geeksforgeeks.org โบ python โบ sort-in-python
sort() in Python - GeeksforGeeks
Python ยท a = [5, 3, 8, 1, 2] a.sort() ... Parameters: key: A function that decides how elements are compared ยท reverse: If True, sorts the list in descending order ยท...
Published: January 13, 2026
YouTube
youtube.com โบ watch
Python: How to Sort Arrays In Ascending and Descending Order (String and Int Arrays) - YouTube
How to Sort Arrays In Ascending and Descending Order In Python (String and Int Arrays)Greetings, today we are here with a video on how to sort the data in yo...
Published: November 10, 2022
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)
Stack Overflow
stackoverflow.com โบ questions โบ 4183506 โบ python-list-sort-in-descending-order
Python list sort in descending order - Stack Overflow
How can I sort this list in descending order? timestamps = [ "2010-04-20 10:07:30", "2010-04-20 10:07:38", "2010-04-20 10:07:52", "2010-04-20 10:0...
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 - Now, to obtain the indices that sort the array in descending order, we first negate the array using -arr and then use argsort() on the negated array to get the indices in descending order.
Upgrad
upgrad.com โบ home โบ tutorials โบ software & tech โบ sort array in python
Effortless Array Sorting in Python: A Step-by-Step Guide
November 13, 2024 - Let's explore the foundational methods Python offers for this purpose. ... reverse (optional): A boolean value. If True, the list is sorted in descending order; if False (default), it's sorted in ascending order.
Programiz
programiz.com โบ python-programming โบ methods โบ list โบ sort
Python List sort() (with Code Visualization)
numbers = [11, 3, 7, 5, 2] # Sort in ascending order numbers.sort() print(f'Ascending order: {numbers}') # Sort in descending order numbers.sort(reverse=True) print(f'Descending order: {numbers}')
Javatpoint
javatpoint.com โบ python-program-to-sort-the-elements-of-an-array-in-descending-order
Python program to sort the elements of an array in descending order - Javatpoint
Learn basics of Python program to sort the elements of an array in descending order
W3Schools
w3schools.com โบ python โบ ref_func_sorted.asp
Python sorted() Function
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... The sorted() function returns a sorted list of the specified iterable object. You can specify ascending or descending order.
NumPy
numpy.org โบ doc โบ stable โบ reference โบ generated โบ numpy.sort.html
numpy.sort โ NumPy v2.5 Manual
Sort order. If True, the returned array will be sorted in descending order. If False or None, the returned array will be sorted in ascending order. Values that are NaN are sorted to the end for both orders.
Mimo
mimo.org โบ glossary โบ python โบ list-sort()
Python List sort(): Master Data Sorting | Learn Now
You can sort the list in descending order by passing reverse=True as an argument: ... This method sorts the list based on the specified sorting criteria, making it one of Python's most commonly used features for data organization.
Tutorialspoint
tutorialspoint.com โบ python โบ python_sort_arrays.htm
Python - Sort Arrays
... The function returns a new list containing all items from the iterable in ascending order. Set reverse parameter to True to get a descending order of items. The sorted() function can be used along with any iterable.