Signup/Sign In

Sorting a NumPy Array - numpy.sort() Function

Posted in Technology   LAST UPDATED: OCTOBER 20, 2021

    In our previous posts we learned what is Numpy and how to create a Numpy array. Now we will see how to sort the values stored in a given Numpy array. First of all, let us look at how to sort the array with the in-built sorted() method.

    Sorting an array using sorted() function:

    Let's create an array arr, having different values and then sort it.

    import numpy as np
    
    arr=np.array([51,65,2,7,100])    # NumPy array arr created
    print(sorted(arr))    # Printing the sorted array

    However, sorted() function is not an efficient method to sort a Numpy array.

    Next, let us now sort the array using Numpy's sort function.


    Using numpy.sort() function:

    Intuitively, numpy.sort() is used to sort the values of a given NumPy array. There is only one argument necessary, that is obviously the array you want to sort. There are some other optional arguments which we will see later, let's first sort one array.

    Following is the syntax for this function:

    numpy.sort(array, ...)

    In the code example below, are creating a NumPy array arr using np.random.randint() function. This array arr has a shape (2, 2) and is filled with values in the range 1 to 50. Then we sort this array using np.sort(arr) and sorted values are printed.

    arr = np.random.randint(1,5) 
    print('Original Array:',arr)
    
    sorted_array = np.sort(arr)
    print('Sorted Array is ',sorted_array)

    As you can see above the array arr is sorted row-wise. The values in each row are compared with each other and sorted.

    Now talking about the arguments, we passed the array to be sorted but there are some optional arguments too. These are:

    1. axis: Axis along which the array needs to be sorted.

    2. kind: The sorting algorithm which should be used to sort the array. The default is quicksort. We can pass mergesort or heapsort as values too.

    3. order: This specifies which field to compare first. This is used in a Structured Numpy array. It can be a single column name or list of column names along which sorting needs to be done.


    Time for some Examples:

    Let's try a few examples to see the optional arguments in action.

    Using axis parameter of numpy.sort() function:

    import numpy as np
    
    a = np.random.randn(2,3)
    
    print("Original array :", a)
    
    ## axis = 0 means sort column wise
    ## axis = 1 means sort row wise
    a_sort=np.sort(a, axis=0)
    
    print("Sorted array :",a_sort)

    Output:

    Original array : [[-0.90271448  1.34187672 -0.36313977]
                      [-1.02900777 -0.56858467 -0.70997479]]
    Sorted array : [[-1.02900777 -0.56858467 -0.70997479]
                    [-0.90271448  1.34187672 -0.36313977]]

    Using kind parameter of numpy.sort() function:

    import numpy as np
    
    b = np.random.randn(10,50)
    
    print("Original Array :",b)
    
    b_sort = np.sort(b, kind='heapsort')
    
    print("Sorted array :", b_sort)

    See the running example below,

    The value for kind parameter can be: mergesort, heapsort, quicksort, stable, etc.

    So, now we have sorted the NumPy array using both sorted() and np.sort() functions. You can also sort NumPy arrays by combining various optional arguments.

    Happy Learning! Stay Curious!

    You may also like:

    Author:
    I am an undergraduate pursuing B.Tech Computer Science, currently in 3rd year.I love to manipulate data to make sense out of it, my interests include machine learning and deep learning.I love to code in python.
    PythonNumpyNumpy Sort
    IF YOU LIKE IT, THEN SHARE IT

    RELATED POSTS