Some sorting example in paython.
>>> sorted([5, 2, 3, 1, 4,8,9,7])
output : [1, 2, 3, 4, 5, 7, 8, 9]
list.sort() method :
>>> a= [5, 2, 3, 1, 4,8,9,7]
>>> a.sort()
>>> a
output : [1, 2, 3, 4, 5, 7, 8, 9]
Key Functions :
>>> sorted(“a key parameter to specify a function to be called on each list element prior to making comparisons”.split(), key=str.lower)
output : [‘a’, ‘a’, ‘be’, ‘called’, ‘comparisons’, ‘each’, ‘element’, ‘function’, ‘key’, ‘list’, ‘making’, ‘on’, ‘parameter’, ‘prior’, ‘specify’, ‘to’, ‘to’, ‘to’]
>>> student_tuples = [
(‘john’, ‘A’, 15),
(‘jane’, ‘B’, 12),
(‘dave’, ‘B’, 10),
]
>>> sorted(student_tuples, key=lambda student: student[2]) # sort by age
output : [(‘dave’, ‘B’, 10), (‘jane’, ‘B’, 12), (‘john’, ‘A’, 15)]