<jupyter_start><jupyter_text>We often need to sort arrays.<jupyter_code># Import the numpy package
import numpy as np<jupyter_output><empty_output><jupyter_text>Here is an array of numbers:<jupyter_code>my_array = np.array([9, 1, 3, 0])
my_array<jupyter_output><empty_output><jupyter_text>Say we wanted to sort this array, from smallest to largest.  To do this, we use `np.sort`:<jupyter_code>sorted_array = np.sort(my_array)
sorted_array<jupyter_output><empty_output><jupyter_text>That's simple enough for numbers, but what happens if we have strings?Here is an array of strings:<jupyter_code>another_array = np.array(['Matthew', 'John', 'Peter'])
another_array<jupyter_output><empty_output><jupyter_text>Notice the `dtype` \- or data type.  It is `<U7`. This means that the arraycontains strings of 7 characters or less.  It's not important for our purposes,but the "U" stands for [Unicode](https://en.wikipedia.org/wiki/Unicode) \-a way that the computer can store characters in strings.What do you think would happen if we sorted `another_array`?If you guessed that the strings get sorted [alphabetically](https://en.wikipedia.org/wiki/Alphabetical_order), you have a virtual gold star.<jupyter_code>np.sort(another_array)<jupyter_output><empty_output><jupyter_text>Remember how an alphabetical sort works.  The string "a" comes before thestring "aa".  "aa" comes before "ab".<jupyter_code>short_strings = np.array(['ab', 'aa', 'a'])
np.sort(short_strings)<jupyter_output><empty_output><jupyter_text>Here's another array of strings:<jupyter_code>further_array = np.array(['Matthew', 'Matt', 'Matty'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11627906976744186, 'fraction_numerical': 0.0036719706242350062, 'mean_word_length': 7.299492385786802, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '327013', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 562, 'n_tokens_neox': 537, 'n_words': 152, 'path': 'notebooks/06/sorting_arrays.ipynb', 'repo_name': 'matthew-brett/cfd-uob', 'size': '1634'}