---
title: "How to use sorting in Numpy?"  
description: "How to use sorting in Numpy?"  
author: "Ravi Vishwakarma"  
published: 2025-11-18  
updated: 2025-11-18  
canonical: https://www.mindstick.com/interview/34415/how-to-use-sorting-in-numpy  
category: "python"  
tags: ["python-3.4", "numpy"]  
reading_time: 3 minutes  

---

# How to use sorting in Numpy?

> [NumPy](https://www.mindstick.com/interview/34414/how-to-search-in-numpy-arrays) provides the `np.sort()` function to sort arrays. It returns a **sorted copy** of the array without changing the original.

## 1. Basic Sorting

```python
import numpy as np

arr = np.array([3, 1, 5, 2, 8])
sorted_arr = np.sort(arr)

print(sorted_arr)
```

## Output:

```plaintext
[1 2 3 5 8]
```

## 2. Sorting a 2D Array

By default, NumPy sorts [rows **individually**](https://www.mindstick.com/interview/34412/how-to-use-numpy-joining-array).

```python
import numpy as np

arr = np.array([[3, 1, 2],
                [9, 7, 4]])

print(np.sort(arr))
```

## Output:

```plaintext
[[1 2 3]
 [4 7 9]]
```

## 3. Sorting by Axis

#### Sort each column

```python
np.sort(arr, axis=0)
```

#### Sort each row

```python
np.sort(arr, axis=1)
```

## 4. Using `np.argsort()` (Get Sorted Indices)

`argsort()` returns **indices** of sorted elements.

```python
arr = np.array([40, 10, 30, 20])
indices = np.argsort(arr)

print(indices)
```

## Output:

```plaintext
[1 3 2 0]
```

You can use it to rearrange the array:

```python
print(arr[indices])
```

## 5. Sorting Structured Arrays (Sort by Field)

```python
data = np.array([(1, 'B'), (3, 'A'), (2, 'C')],
                dtype=[('id', int), ('name', 'U1')])

print(np.sort(data, order='id'))
```

## 6. In-place Sorting

You can sort an array without creating a copy:

```python
arr.sort()
print(arr)
```

## 7. Sorting in Descending Order

NumPy does not have a direct parameter like `reverse=True`,\
but you can reverse after sorting:

```python
arr = np.array([3, 1, 4])
desc = np.sort(arr)[::-1]

print(desc)
```

## Summary

| Function | Purpose |
| --- | --- |
| `np.sort()` | Returns sorted copy |
| `arr.sort()` | Sorts in-place |
| `np.argsort()` | Returns indices of sorted order |
| `np.sort(arr, axis=0/1)` | Sort rows or columns |
| `np.sort(order='field')` | Sort structured arrays |

## Answers

### Answer by Ravi Vishwakarma

> [NumPy](https://www.mindstick.com/interview/34414/how-to-search-in-numpy-arrays) provides the `np.sort()` function to sort arrays. It returns a **sorted copy** of the array without changing the original.

## 1. Basic Sorting

```python
import numpy as np

arr = np.array([3, 1, 5, 2, 8])
sorted_arr = np.sort(arr)

print(sorted_arr)
```

## Output:

```plaintext
[1 2 3 5 8]
```

## 2. Sorting a 2D Array

By default, NumPy sorts [rows **individually**](https://www.mindstick.com/interview/34412/how-to-use-numpy-joining-array).

```python
import numpy as np

arr = np.array([[3, 1, 2],
                [9, 7, 4]])

print(np.sort(arr))
```

## Output:

```plaintext
[[1 2 3]
 [4 7 9]]
```

## 3. Sorting by Axis

#### Sort each column

```python
np.sort(arr, axis=0)
```

#### Sort each row

```python
np.sort(arr, axis=1)
```

## 4. Using `np.argsort()` (Get Sorted Indices)

`argsort()` returns **indices** of sorted elements.

```python
arr = np.array([40, 10, 30, 20])
indices = np.argsort(arr)

print(indices)
```

## Output:

```plaintext
[1 3 2 0]
```

You can use it to rearrange the array:

```python
print(arr[indices])
```

## 5. Sorting Structured Arrays (Sort by Field)

```python
data = np.array([(1, 'B'), (3, 'A'), (2, 'C')],
                dtype=[('id', int), ('name', 'U1')])

print(np.sort(data, order='id'))
```

## 6. In-place Sorting

You can sort an array without creating a copy:

```python
arr.sort()
print(arr)
```

## 7. Sorting in Descending Order

NumPy does not have a direct parameter like `reverse=True`,\
but you can reverse after sorting:

```python
arr = np.array([3, 1, 4])
desc = np.sort(arr)[::-1]

print(desc)
```

## Summary

| Function | Purpose |
| --- | --- |
| `np.sort()` | Returns sorted copy |
| `arr.sort()` | Sorts in-place |
| `np.argsort()` | Returns indices of sorted order |
| `np.sort(arr, axis=0/1)` | Sort rows or columns |
| `np.sort(order='field')` | Sort structured arrays |


---

Original Source: https://www.mindstick.com/interview/34415/how-to-use-sorting-in-numpy

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
