NumPy Array Sort

Sorting means arranging array elements in a particular order. In NumPy Array Sort, we can sort numbers in ascending or descending order and arrange data in 1-D and 2-D arrays. The sort() function is useful for organizing marks, sales figures, prices, and other numerical data.

1. What is Sorting in NumPy?

Sorting arranges array elements in order.

There are two common sorting orders:

  • Ascending Order: Smallest to largest.
  • Descending Order: Largest to smallest.

NumPy provides the sort() function to sort array elements.

2. Sorting a 1-D Array

You can use np.sort() to arrange elements of a one-dimensional array in ascending order.

Example:

import numpy as np

marks = np.array([85, 45, 95, 60, 75])

result = np.sort(marks)

print(result)

Output:

[45 60 75 85 95]

Explanation:

  • np.sort(marks) sorts the marks from smallest to largest.
  • The original array remains unchanged.
  • The sorted result is returned as a new array.

3. Sorting an Array in Descending Order

To sort an array in descending order, first sort it in ascending order and then reverse the result using [::-1].

Example:

import numpy as np

marks = np.array([85, 45, 95, 60, 75])

result = np.sort(marks)[::-1]

print(result)

Output:

[95 85 75 60 45]

Explanation:

  • np.sort(marks) arranges the values in ascending order.
  • [::-1] reverses the sorted array.
  • The final result is in descending order.

4. Sorting a 2-D Array

NumPy can also sort elements inside a two-dimensional array.

Example:

import numpy as np

marks = np.array([
    [85, 45, 95],
    [60, 75, 50]
])

result = np.sort(marks)

print(result)

Output:

[[45 85 95]
 [50 60 75]]

Explanation:

  • Each row is sorted independently by default.
  • The first row becomes [45, 85, 95].
  • The second row becomes [50, 60, 75].

5. Sorting a 2-D Array Using axis=0

The axis=0 parameter sorts values vertically, column by column.

Example:

import numpy as np

sales = np.array([
    [500, 300, 700],
    [200, 600, 400],
    [800, 100, 500]
])

result = np.sort(sales, axis=0)

print(result)

Output:

[[200 100 400]
 [500 300 500]
 [800 600 700]]

Explanation:

  • axis=0 sorts each column separately.
  • The first column becomes 200, 500, 800.
  • The second column becomes 100, 300, 600.
  • The third column becomes 400, 500, 700.

6. Sorting a 2-D Array Using axis=1

The axis=1 parameter sorts values horizontally, row by row.

Example:

import numpy as np

sales = np.array([
    [500, 300, 700],
    [200, 600, 400],
    [800, 100, 500]
])

result = np.sort(sales, axis=1)

print(result)

Output:

[[300 500 700]
 [200 400 600]
 [100 500 800]]

Explanation:

  • axis=1 sorts each row separately.
  • The first row becomes 300, 500, 700.
  • The second row becomes 200, 400, 600.
  • The third row becomes 100, 500, 800.

7. Sorting an Array Without Changing the Original Array

The np.sort() function returns a sorted copy. It does not change the original array.

Example:

import numpy as np

prices = np.array([500, 200, 800, 300])

sorted_prices = np.sort(prices)

print("Original Array:", prices)
print("Sorted Array:", sorted_prices)

Output:

Original Array: [500 200 800 300]
Sorted Array: [200 300 500 800]

Explanation:

  • prices contains the original values.
  • np.sort(prices) creates a sorted result.
  • Both arrays remain separate.

8. Understanding the argsort() Function

The argsort() function returns the indexes that would arrange an array in ascending order. It returns index positions, not sorted values.

Example:

import numpy as np

marks = np.array([85, 45, 95, 60])

result = np.argsort(marks)

print(result)

Output:

[1 3 0 2]

Explanation:

The original array is:

[85 45 95 60]

The sorted order would be:

[45 60 85 95]

Their original index positions are:

  • 45 is at index 1.
  • 60 is at index 3.
  • 85 is at index 0.
  • 95 is at index 2.

Therefore, argsort() returns [1, 3, 0, 2].

9. Difference Between sort() and argsort()

Featuresort()argsort()
PurposeSorts array valuesReturns sorting indexes
ResultSorted valuesIndex positions
Example[45, 60, 85][1, 2, 0]
UseGet sorted dataFind the original positions of sorted values

Student Task

Create a NumPy array using the following data:

employee_salary = np.array([35000, 25000, 50000, 30000, 45000])

Complete these tasks:

  1. Sort employee salaries in ascending order.
  2. Sort employee salaries in descending order.
  3. Print the original salary array.
  4. Use argsort() to find the index positions of sorted salaries.
  5. Create a 2-D array containing monthly sales and sort it using axis=0.
  6. Sort the same 2-D array using axis=1.

Final Student Challenge: Create a Sorted Sales Report

A company has recorded sales figures for three employees across three months.

import numpy as np

sales = np.array([
    [45000, 30000, 50000],
    [25000, 55000, 35000],
    [60000, 40000, 20000]
])

Complete these tasks:

  1. NumPy Sort each employee’s monthly sales in ascending order.
  2. NumPy Sort each employee’s monthly sales in descending order.
  3. Sort sales vertically using axis=0.
  4. Find the index positions of sorted sales using argsort().
  5. Display the original sales array to confirm it remains unchanged.

Key Takeaways

  • np.sort() arranges array elements in ascending order by default.
  • [::-1] reverses a sorted array to produce descending order.
  • axis=0 sorts a 2-D array column by column.
  • axis=1 sorts a 2-D array row by row.
  • np.sort() does not modify the original array.
  • np.argsort() returns the index positions that would sort an array.

Frequently Asked Questions (FAQs)

1. What is NumPy Array Sort in Python?

NumPy Array Sort is used to arrange the elements of an array in a specific order. NumPy provides the sort() function to sort array elements in ascending order by default.

2. What is the NumPy sort() function used for?

The NumPy sort() function is used to arrange array elements in ascending order. It works with both one-dimensional and multidimensional arrays.

Example: np.sort(arr)

3. How do you sort a NumPy Array in ascending order?

You can use the np.sort() function to arrange NumPy Array elements from smallest to largest.

Example:

import numpy as np

arr = np.array([40, 10, 30, 20])

result = np.sort(arr)

print(result)

Output:

[10 20 30 40]

4. How do you perform NumPy 2D Array Sorting?

You can use the np.sort() function to sort elements of a 2D NumPy Array. By default, it sorts each row independently along the last axis.

Example: np.sort(arr, axis=1)

5. What is the difference between NumPy sort() and argsort()?

The sort() function returns the sorted elements of an array, whereas argsort() returns the indices that would arrange the array elements in sorted order.

6. How do you sort a NumPy Array in descending order?

You can use the np.sort() function followed by [::-1] to arrange array elements in descending order.

Example: np.sort(arr)[::-1]

7. Can we sort a NumPy Array containing duplicate values?

Yes, NumPy Array Sorting supports duplicate values. The np.sort() function arranges all elements in ascending order while keeping duplicate values in the sorted result.

8. Does NumPy sort() Function change the original array?

No, np.sort() returns a sorted copy of the array and does not modify the original array. To sort the original array directly, you can use the sort() method: arr.sort().

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top