Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NumPy sort function returns None

I have one simple program below:

import numpy as np

arr = np.random.randn(8)
new = arr.sort()
new1 = np.sort(arr)
print new
print new1

I expected the two new arrays to be the same a sorted array, but instead, new is None, new1 is what I expected, what is the difference between two methods to sort?

like image 538
Dogod Avatar asked Jul 11 '17 01:07

Dogod


1 Answers

From the documentation for numpy.ndarray.sort:

Sort an array, in-place.

If you want a sorted copy of the original array, rather than sorting in place, you should use numpy.sort, which returns a copy, as you saw.

like image 122
ngoldbaum Avatar answered Sep 18 '22 19:09

ngoldbaum