From d359b1bea80cd30c5fbb6921af6d758a9b62d673 Mon Sep 17 00:00:00 2001 From: avdhoottt Date: Thu, 30 Oct 2025 14:51:01 +0530 Subject: [PATCH 1/4] Sort entry --- .../numpy/concepts/ndarray/terms/sort/sort.md | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 content/numpy/concepts/ndarray/terms/sort/sort.md diff --git a/content/numpy/concepts/ndarray/terms/sort/sort.md b/content/numpy/concepts/ndarray/terms/sort/sort.md new file mode 100644 index 00000000000..7469961c78f --- /dev/null +++ b/content/numpy/concepts/ndarray/terms/sort/sort.md @@ -0,0 +1,71 @@ +--- +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](https://www.codecademy.com/resources/docs/numpy/ndarray) in-place in ascending order along a specified axis. By default, it sorts in ascending order. + +## Syntax of Numpy `.sort()` + +```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) +``` + +The above code will result in the following output: + +```shell +['xxx' 'sss' 'ddd' 'aaa'] +``` + +## Codebyte Example + +In the following codebyte example, a NumPy array `nf` is created with integers, sorted in ascending order using NumPy's `.sort()` method, and then reversed to display the values in descending order: + +```codebyte/python +import numpy as np + +nf = np.array([5, 1, 9, 3, 2, 8]) + +# Sort the array in-place using NumPy's sort method +nf.sort() +print("Ascending:", nf) + +# Reverse for descending order +nf = nf[::-1] +print("Descending:", nf) +``` From 17c865b0cbe41b55a461252a019aba74a552f552 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Thu, 30 Oct 2025 17:23:14 +0530 Subject: [PATCH 2/4] minor fixes --- content/numpy/concepts/ndarray/terms/sort/sort.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/numpy/concepts/ndarray/terms/sort/sort.md b/content/numpy/concepts/ndarray/terms/sort/sort.md index 7469961c78f..4d4f50a4d1e 100644 --- a/content/numpy/concepts/ndarray/terms/sort/sort.md +++ b/content/numpy/concepts/ndarray/terms/sort/sort.md @@ -1,6 +1,6 @@ --- Title: '.sort()' -Description: 'Sorts the array in-place along the specified axis' +Description: 'Sorts the array in-place along the specified axis.' Subjects: - 'Code Foundations' - 'Computer Science' @@ -14,7 +14,7 @@ CatalogContent: - 'paths/computer-science' --- -The **`.sort()`** method sorts a NumPy [array](https://www.codecademy.com/resources/docs/numpy/ndarray) in-place in ascending order along a specified axis. By default, it sorts in ascending order. +The **`.sort()`** method sorts a NumPy [array](https://www.codecademy.com/resources/docs/numpy/ndarray) in-place in ascending order along a specified axis. It modifies the original array and does not return a new one. ## Syntax of Numpy `.sort()` From daa3071532ab8740c6b038570265e4f2c2144341 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Thu, 30 Oct 2025 17:27:37 +0530 Subject: [PATCH 3/4] Update sort.md with new content structure From fb32325458833ca64af0a40f41f8b7f8b5cac67b Mon Sep 17 00:00:00 2001 From: avdhoottt Date: Thu, 30 Oct 2025 20:00:31 +0530 Subject: [PATCH 4/4] Format --- .../numpy/concepts/ndarray/terms/sort/sort.md | 142 +++++++++--------- 1 file changed, 71 insertions(+), 71 deletions(-) diff --git a/content/numpy/concepts/ndarray/terms/sort/sort.md b/content/numpy/concepts/ndarray/terms/sort/sort.md index 4d4f50a4d1e..097a028cf2c 100644 --- a/content/numpy/concepts/ndarray/terms/sort/sort.md +++ b/content/numpy/concepts/ndarray/terms/sort/sort.md @@ -1,71 +1,71 @@ ---- -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](https://www.codecademy.com/resources/docs/numpy/ndarray) in-place in ascending order along a specified axis. It modifies the original array and does not return a new one. - -## Syntax of Numpy `.sort()` - -```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) -``` - -The above code will result in the following output: - -```shell -['xxx' 'sss' 'ddd' 'aaa'] -``` - -## Codebyte Example - -In the following codebyte example, a NumPy array `nf` is created with integers, sorted in ascending order using NumPy's `.sort()` method, and then reversed to display the values in descending order: - -```codebyte/python -import numpy as np - -nf = np.array([5, 1, 9, 3, 2, 8]) - -# Sort the array in-place using NumPy's sort method -nf.sort() -print("Ascending:", nf) - -# Reverse for descending order -nf = nf[::-1] -print("Descending:", nf) -``` +--- +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](https://www.codecademy.com/resources/docs/numpy/ndarray) in-place in ascending order along a specified axis. It modifies the original array and does not return a new one. + +## Syntax of Numpy `.sort()` + +```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) +``` + +The above code will result in the following output: + +```shell +['xxx' 'sss' 'ddd' 'aaa'] +``` + +## Codebyte Example + +In the following codebyte example, a NumPy array `nf` is created with integers, sorted in ascending order using NumPy's `.sort()` method, and then reversed to display the values in descending order: + +```codebyte/python +import numpy as np + +nf = np.array([5, 1, 9, 3, 2, 8]) + +# Sort the array in-place using NumPy's sort method +nf.sort() +print("Ascending:", nf) + +# Reverse for descending order +nf = nf[::-1] +print("Descending:", nf) +```