Skip to content

Add sort entry #7340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions content/numpy/concepts/ndarray/terms/sort/sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
Title: '.sort()'
Description: 'Sorts the array in-place along the specified axis'
Subjects:
- 'Code Foundations'
- 'Computer Science'
Tags:
- 'Arrays'
- 'Elements'
- 'Methods'
- 'NumPy'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

The **`.sort()`** method sorts a NumPy array in-place in ascending order along a specified axis. By default, it sorts in ascending order.

## Syntax

```pseudo
ndarray.sort(axis=-1, kind=None, order=None)
```

**Parameters:**

- `axis`: Axis along which to sort. Default is `-1` (last axis). Use `None` to sort the flattened array.
- `kind`: Sorting algorithm: `'quicksort'` (default), `'mergesort'`, `'heapsort'`, or `'stable'`.
- `order`: Only for structured arrays. Specifies the field(s) to sort by.

## Example

In this example, the `.sort()` method is used to sort a NumPy array in descending order:

```py
# Import NumPy
import numpy as np

# Create a NumPy array
nf = np.array(['aaa', 'xxx', 'ddd', 'sss'])

# Sort in descending order
nf.sort() # Sorts in ascending order
nf = nf[::-1] # Reverses to get descending order

print(nf)
```

We will get the following result:

```shell
['xxx' 'sss' 'ddd' 'aaa']
```

## Codebyte Example

In the following codebyte example, a NumPy array `nf` is created with strings of varying lengths, and then sorted in descending order based on string length:

```codebyte/python
import numpy as np

nf = np.array(['aaa', 'cccc', 'xxxxx', 'bb', 'yyyyyyy', 'p'])

sorted_nf = np.array(sorted(nf, key=len, reverse=True))

print(sorted_nf)
```