<jupyter_start><jupyter_text>Basic Python Function- `sorted(iterable, *, key=None, reverse=False)`: [ref1: official doc](https://docs.python.org/3/library/functions.htmlsorted) | [ref2: Stack Overflow](https://stackoverflow.com/a/4233482/8280662)- `list.sort(*, key=None, reverse=False)`: [ref](https://docs.python.org/3/library/stdtypes.htmllist.sort)[Sorting HowTo official doc](https://docs.python.org/3/howto/sorting.htmlsortinghowto)  `sorted()``sorted(iterable, /, *, key=None, reverse=False)`  Sort Tuple by multiple KeysSort by 2 keys in Python: A key can be a function that returns a `tuple`:<jupyter_code>from collections import namedtuple

student_tuples = ( 
     ('john', 'A', 15),
     ('jane', 'C', 12),
     ('jane', 'B', 13),
     ('coco', 'B', 12),
     ('dave', 'B', 10),
     ('dave', 'A', 11)
)
# v1
print(student_tuples[0][:3])

sorted(student_tuples, key=lambda x: (x[0], x[1], x[2]))  # sort by (name, grade, value) one by one
print(student_tuples[0][::-1])
print()
sorted(student_tuples, key=lambda x: (x[2], x[1], x[0]), reverse=True)<jupyter_output>(15, 'A', 'john')<jupyter_text>Call `sorted(iterable, key: NoneType=None)` with a collection as iterable. For `key`, create a lambda function that takes an element as a parameter and returns a 2-tuple of mapped to values.<jupyter_code>a_list = ["aaa", "cc", "bb"]

new_list = sorted(a_list, key=lambda x: (len(x), x))
new_list<jupyter_output><empty_output><jupyter_text>Sort Dictionary by value and key<jupyter_code>from collections import Counter

words = ["i","love","leetcode","i","love","coding"]

counter = Counter(words)
counter
# sort dictionary directly will only sort by keys
sorted(counter)
# use `.items()` to get a list of tuples
sorted_counter = sorted(counter.items(), key=lambda x: (-x[1], x[0]))
sorted_counter
# we can turn it back to dictionary
dict(sorted_counter)<jupyter_output><empty_output><jupyter_text>`list.sort()``list.sort(*, key=None, reverse=False)` | [ref](https://docs.python.org/3/library/stdtypes.htmllist.sort)<jupyter_code>import random

import numpy as np


input = list(zip(range(10), np.arange(10)*10))

random.seed(2)
random.shuffle(input)

input
from typing import List, Tuple

def my_sort(v: List[Tuple]):
    v.sort(reverse=True)  # this will change the input inplace
    return v

my_sort(input)
input<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17006802721088435, 'fraction_numerical': 0.023809523809523808, 'mean_word_length': 5.920588235294118, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 5, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '294627', 'license': 'Apache-2.0', 'n_tokens_mistral': 895, 'n_tokens_neox': 849, 'n_words': 208, 'path': 'Python/sort.ipynb', 'repo_name': 'Chet-Sheng/Speedy', 'size': '2352'}