Skip to main content

Sorting

  • Stability in sorting: Order of occurrence is maintained when values are same
  • sort() and sorted() are stable
  • both use Tim Sort (Merge + Insertion) internally
# Works for list
arr.sort() # sorts in place, ASC
arr.sort(reverse=True) # DESC

# works for any iterable - list, tuple, dictionary, etc.
t = sorted(arr) # creates new list
t = sorted(arr, reverse=True)

# sorting with a lambda function
def myFun(s):
return len(s)
arr.sort(key=myFun)
note

sort() only works for array and sorted() works for any iterable

Objects

class Point:
def __init__(self, x, y):
self.x = x
self.y = y

def myFun(p):
return p.x

l = [Point(2,3), Point(5,6)]
l.sort(key=myFun)