
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
Python - numpy.sort() Function
In Python, numpy.sort() function is used to sort the elements of a NumPy array along a specified axis. This function sorts the array in ascending order by default, but can also sort in descending order by modifying the array or using other approaches.
It works with multi-dimensional arrays and provides flexibility in sorting either entire arrays or individual rows/columns.
Syntax
Following is the syntax of the Python numpy.sort() function −
numpy.sort(a, axis=-1, kind=None, order=None)
Parameters
Following are the parameters of the Python numpy.sort() function −
- a: The input array to be sorted.
- order (optional): Specifies the field order for structured arrays. For example, sorting by multiple fields in a structured array.
- stable(optional): This is sorting stability. If True, the returned array will maintain the relative order of arr values which compare as equal. If False or None, this is not guaranteed. Internally, this option selects kind='stable'. Default value is None.
-
axis (optional): The axis along which to sort −
- -1 (default): Sorts along the last axis.
- 0: Sorts along columns (vertically).
- 1: Sorts along rows (horizontally).
- kind (optional): Specifies the sorting algorithm. Options include −
- quicksort (default): Fast but not stable.
- mergesort: Stable and maintains the relative order of equal elements.
- heapsort: Memory-efficient but slower than 'quicksort'.
Return Type
This function returns a sorted array, which is a new array if the input is not modified in place.
Example
Following is a basic example to sort a 1-dimensional NumPy array using the Python numpy.sort() function −
import numpy as np my_array = np.array([25, 5, 15, 10]) sorted_array = np.sort(my_array) print("Original Array:", my_array) print("Sorted Array:", sorted_array)
Output
Following is the output of the above code −
Original Array: [25 5 15 10] Sorted Array: [ 5 10 15 25]
Example: Sorting Along Axis
We can sort along a specific axis in a multi-dimensional array using the axis parameter. The following example demonstrates sorting rows (axis=1) and columns (axis=0) −
import numpy as np my_array = np.array([[8, 2, 7], [4, 9, 1]]) sorted_row = np.sort(my_array, axis=1) sorted_col = np.sort(my_array, axis=0) print("Original Array:\n", my_array) print("Sorted by Rows:\n", sorted_row) print("Sorted by Columns:\n", sorted_col)
Output
Following is the output of the above code −
Original Array: [[8 2 7] [4 9 1]] Sorted by Rows: [[2 7 8] [1 4 9]] Sorted by Columns: [[4 2 1] [8 9 7]]
Example: Sorting Using Custom Data Type
The order parameter is used for structured arrays. Here's an example to sort by specific fields −
import numpy as np my_array = np.array([(1, 'Zebra', 100), (2, 'Apple', 50), (3, 'Monkey', 75)], dtype=[('id', int), ('name', 'U10'), ('score', int)]) sorted_by_name = np.sort(my_array, order='name') print("Original Array:\n", my_array) print("Sorted by Name:\n", sorted_by_name)
Output
Following is the output of the above code −
Original Array: [(1, 'Zebra', 100) (2, 'Apple', 50) (3, 'Monkey', 75)] Sorted by Name: [(2, 'Apple', 50) (3, 'Monkey', 75) (1, 'Zebra', 100)]