Skip to content

Latest commit

 

History

History
37 lines (29 loc) · 872 Bytes

SortingArrays.md

File metadata and controls

37 lines (29 loc) · 872 Bytes

Sorting Arrays

  • Sorting = putting elements in an ordered sequence (like numeric, alphabetic, ascending, descending, etc.)
  • sort() -> function of NumPy ndarray object, that sorts a specified array

ab = np.array([5, 3, 4, 0, 2, 1])
print(np.sort(ab))


Output:

[0 1 2 3 4 5]

NOTE: sort() returns a copy of the array, leaving the original array unchanged.

Sorting alphabetically

ac = np.array(['pear', 'banana', 'apple', 'mango'])
np.sort(ac)


Output

['apple', 'banana', 'mango', 'pear']


Sorting boolean array

ad = np.array([True, False, True, True, False, False])
np.sort(ad)


Output:

[False False False True True True]

Sorting 2D Array

ae = np.array([[21, 10, 3], [8, -6, 4], [3, 0, -2]])
np.sort(ae)


Output:

[[3 10 21] [-6 4 8] [-2 0 3]]