To sort NumPy arrays, I typically use the numpy.sort() function or the numpy.ndarray.sort() method. Both approaches effectively sort the elements in an array, but they differ in their usage. The numpy.sort() function returns a new sorted array, while ndarray.sort() sorts the array in place, modifying the original array.
For example, if I have an array arr = np.array([3, 1, 2]), using sorted_arr = np.sort(arr) gives me a new array [1, 2, 3], while arr.sort() changes arr directly to [1, 2, 3]. The choice between these methods often depends on whether I need to retain the original array or not.
Additionally, I can specify the axis parameter to sort along a specific dimension, and the kind parameter to choose the sorting algorithm, such as 'quick', 'mergesort', or 'heapsort'. Each algorithm has its own performance characteristics, so for large datasets, I might prefer 'mergesort' for its stability.