Menu

Numpy.sort() in python

The numpy.sort() function in python is used to sort the array along a specified axis.

Written by Selva Prabhakaran | 2 min read

The np.sort() function is used to sort the array along a specified axis.

Numpy.sort (a, axis=- 1, kind=None, order=None)

  • Purpose: This function is used for sorting the array.
  • Parameters:
    • arr:a:array_like array to be sorted.
    • axis: None or int,optional Axis on which we perform the arithmetic mean if specified. otherwise, the arr will be flattened.
    • kind: {‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’}, optional Kind of sorting algorithm that should be used on the array if required.
    • order: str or list of str, optional which fields should be compared first.
  • Returns:
    • sorted array:ndarray_ Returns array of same type and shape as a
python
# Import Packages
import numpy as np

Numpy Sort Function

The numpy.sort() function takes in the array as one of the required arguments and returns a copy of the sorted array. Let’s take an example to understand this.

1. Sort 1-D array

Let’s see, how to sort a 1-D numpy array.

Example 1: Sort a 1-D numpy array with default parameters.

python
# Create a 1-D array
a = np.array([3,6,5,2,1])
python
# Apply sort method on the above array
sort_a = np.sort(a)
python
# Print the sorted array
print(sort_a)
python
[1 2 3 5 6]

The above array is sorted in ascending order. because you have not mentioned any parameters.

2. Sort on 2-D array

Let’s see, sort of a 2-D numpy array using default parameters.

Example 1: Sort a 2-D numpy array with default parameters.

python
# Create a 2-D array
a = np.array([[2,5,4],[7,4,9]])
python
# Apply sort method on the above array
sort_a = np.sort(a)
python
# print sorted array
print(sort_a)
python
[[2 4 5]
 [4 7 9]]

The array is sorted in ascending order along axis 0 as axis 0 is the default parameter.

Example 2: Sort a 2-D numpy array with axis = 0

python
# Create a 2-D array
a = np.array([[2,5,4,0],[7,4,9,1]])
python
# Apply sort method on the above array
sort_a = np.sort(a, kind = 'quicksort', axis = 0)
python
# print sorted array
print(sort_a)
python
[[2 4 4 0]
 [7 5 9 1]]

array = [[2,5,4,0],[7,4,9,1]]

sub array1 = [2,5,4,0]
sub array2 = [7,4,9,1]

  • The sort() method in this example compares the sub array1 1st column value with sub array2 1st column value.
  • Which among the compared values are small that will be sorted first.
  • The same repeats with all column values in sub arrays.

Example 3: Sort a 2-D numpy array with axis = 1

python
# Create a 2-D array
a = np.array([[2,5,4,0],[7,4,9,1]])
python
# Apply sort method on the above array
sort_a = np.sort(a, kind = 'quicksort', axis = 1)
python
# print sorted array
print(sort_a)
python
[[0 2 4 5]
 [1 4 7 9]]

Here, The sort() method is performed on entire sub array1 and sorts the values in that sub array1 in ascending order.

The same applies to sub array2

3.Test your knowledge

Q1: Can you perform a sort operation on 1-D array when axis=1?

Ans: No, you cannot perform a sort operation on 1-D array when axis=1. It returns a error.

Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Related Course
Master Python — Hands-On
Join 5,000+ students at edu.machinelearningplus.com
Explore Course
Free Callback - Limited Slots
Not Sure Which Course to Start With?
Talk to our AI Counsellors and Practitioners. We'll help you clear all your questions for your background and goals, bridging the gap between your current skills and a career in AI.
10-digit mobile number
📞
Thank You!
We'll Call You Soon!
Our learning advisor will reach out within 24 hours.
(Check your inbox too — we've sent a confirmation)
⚡ Before you go

Python.
SQL. NumPy.
All free.

Get the exact 10-course programming foundation that Data Science professionals use.

🐍
Core Python — from first line to expert level
📈
NumPy & Pandas — the #1 libraries every DS job needs
🗃️
SQL Levels I–III — basics to Window Functions
📄
Real industry data — Jupyter notebooks included
R A M S K
57,000+ students
★★★★★ Rated 4.9/5
⚡ Before you go
Python. SQL.
All Free.
R A M S K
57,000+ students  ★★★★★ 4.9/5
Get Free Access Now
10 courses. Real projects. Zero cost. No credit card.
New learners enrolling right now
🔒 100% free ☕ No spam, ever ✓ Instant access
🚀
You're in!
Check your inbox for your access link.
(Check Promotions or Spam if you don't see it)
Or start your first course right now:
Start Free Course →
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science