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
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])
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.
Problem with sorting in descending order
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=' More on reddit.com
python - How to sort in descending order with numpy? - Stack Overflow
There's no reverse option in NumPy's sort or argsort functions because reversing an array is so efficient (it just changes the strides, no data need be copied). A solution via sorting -A would work, but that creates two new arrays instead of one. More on stackoverflow.com
Can we sort Numpy arrays in reverse order?
Question In the context of this exercise, can we sort Numpy arrays in reverse order? Answer In Numpy, the np.sort() function does not allow us to sort an array in descending order. Instead, we can reverse an array utilizing list slicing in Python, after it has been sorted in ascending order. More on discuss.codecademy.com
Sorting 2d Array
Posted by u/PhantomWizard2099 - 2 votes and 14 comments More on reddit.com
how to sort numpy array in descending order
04:15
Efficiently sorting 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 ...
08:44
Python NumPy Tutorial For Beginners - NumPy Sorting Arrays | How ...
Sorting Numpy Arrays The Right Way
NumPy
numpy.org › devdocs › reference › generated › numpy.sort.html
numpy.sort — NumPy v2.6.dev0 Manual
For numerical sorts, NaT and NaN always sort to the end of the array for both ascending and descending sort order.
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.
NumPy
numpy.org › doc › stable › reference › generated › numpy.argsort.html
numpy.argsort — 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.
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 to Add a Column to a NumPy Array
Codegive
codegive.com › blog › numpy_sort_in_descending_order.php
Numpy sort in descending order
NumPy provides powerful tools for ... np.sort() (which returns a new sorted array) and then reverse the order of this newly sorted array using array slicing [::-1]....
Vultr Docs
docs.vultr.com › python › third-party › numpy › sort
Python Numpy sort() - Sort Elements | Vultr Docs
December 31, 2024 - Here, array is first sorted in ascending order and then reversed to get [9, 6, 5, 4, 3, 2, 1, 1] in descending order. Create a two-dimensional array. Sort along a specified axis. ... array_2d = np.array([[12, 15], [10, 1]]) sorted_array_2d = np.sort(array_2d, axis=0) print(sorted_array_2d) Explain Code · The code sorts array_2d along each column (axis=0), reshuffling the rows to maintain order along columns, resulting in [[10, 1], [12, 15]]. Understand that NumPy supports various sorting algorithms such as 'quicksort', 'mergesort', and 'heapsort'.
DataCamp
datacamp.com › doc › numpy › sorting-arrays
NumPy Sorting Arrays
numpy.sort(a, axis=-1, kind='quicksort', order=None) 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.
Codegive
codegive.com › blog › numpy_sort_array_descending.php
Numpy sort array descending
NumPy, the fundamental package for numerical computation in Python, provides powerful and efficient tools for array manipulation, including sorting. While NumPy's primary sorting functions (np.sort and ndarray.sort) by default sort in ascending order, achieving a descending sort is straightforward using a combination of these functions and array slicing.