{ "cells": [ { "cell_type": "markdown", "id": "06ac89df", "metadata": {}, "source": [ "# Sort\n", "\n", "As in Python’s `list`, NumPy arrays can be sorted in-place using the [numpy.sort](https://numpy.org/doc/stable/reference/generated/numpy.sort.html) method. You can sort any one-dimensional section of values in a multidimensional array in place along an axis by passing the axis number to sort:" ] }, { "cell_type": "code", "execution_count": 1, "id": "a4099946", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T14:57:14.908069Z", "iopub.status.busy": "2026-05-17T14:57:14.907839Z", "iopub.status.idle": "2026-05-17T14:57:14.955254Z", "shell.execute_reply": "2026-05-17T14:57:14.954891Z", "shell.execute_reply.started": "2026-05-17T14:57:14.908053Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[0.7740598 , 0.20912608, 0.17613129],\n", " [0.2691818 , 0.84813834, 0.6284038 ],\n", " [0.61870574, 0.86602031, 0.77810677],\n", " [0.58221564, 0.18867566, 0.61433336],\n", " [0.19384893, 0.97196918, 0.58002664],\n", " [0.56652261, 0.968951 , 0.0066615 ],\n", " [0.56644055, 0.47526286, 0.8142787 ]])" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "\n", "rng = np.random.default_rng()\n", "data = rng.random((7, 3))\n", "\n", "data" ] }, { "cell_type": "code", "execution_count": 2, "id": "b2d7e426", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T14:57:14.955765Z", "iopub.status.busy": "2026-05-17T14:57:14.955663Z", "iopub.status.idle": "2026-05-17T14:57:14.959109Z", "shell.execute_reply": "2026-05-17T14:57:14.958871Z", "shell.execute_reply.started": "2026-05-17T14:57:14.955756Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[0.19384893, 0.18867566, 0.0066615 ],\n", " [0.2691818 , 0.20912608, 0.17613129],\n", " [0.56644055, 0.47526286, 0.58002664],\n", " [0.56652261, 0.84813834, 0.61433336],\n", " [0.58221564, 0.86602031, 0.6284038 ],\n", " [0.61870574, 0.968951 , 0.77810677],\n", " [0.7740598 , 0.97196918, 0.8142787 ]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.sort(0)\n", "\n", "data" ] }, { "cell_type": "markdown", "id": "bccf7aeb", "metadata": {}, "source": [ "`np.sort`, on the other hand, returns a sorted copy of an array instead of changing the array in place:" ] }, { "cell_type": "code", "execution_count": 3, "id": "4d6e17da", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T14:57:14.959673Z", "iopub.status.busy": "2026-05-17T14:57:14.959578Z", "iopub.status.idle": "2026-05-17T14:57:14.961964Z", "shell.execute_reply": "2026-05-17T14:57:14.961734Z", "shell.execute_reply.started": "2026-05-17T14:57:14.959665Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[0.0066615 , 0.18867566, 0.19384893],\n", " [0.17613129, 0.20912608, 0.2691818 ],\n", " [0.47526286, 0.56644055, 0.58002664],\n", " [0.56652261, 0.61433336, 0.84813834],\n", " [0.58221564, 0.6284038 , 0.86602031],\n", " [0.61870574, 0.77810677, 0.968951 ],\n", " [0.7740598 , 0.8142787 , 0.97196918]])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sort(data, axis=1)" ] }, { "cell_type": "code", "execution_count": 4, "id": "97af8303", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T14:57:14.962477Z", "iopub.status.busy": "2026-05-17T14:57:14.962390Z", "iopub.status.idle": "2026-05-17T14:57:14.964874Z", "shell.execute_reply": "2026-05-17T14:57:14.964505Z", "shell.execute_reply.started": "2026-05-17T14:57:14.962470Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[0.19384893, 0.18867566, 0.0066615 ],\n", " [0.2691818 , 0.20912608, 0.17613129],\n", " [0.56644055, 0.47526286, 0.58002664],\n", " [0.56652261, 0.84813834, 0.61433336],\n", " [0.58221564, 0.86602031, 0.6284038 ],\n", " [0.61870574, 0.968951 , 0.77810677],\n", " [0.7740598 , 0.97196918, 0.8142787 ]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.13 Kernel", "language": "python", "name": "python313" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.0" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }