Add compat module with a sorted function.
[matthijs/upstream/mobilegtd.git] / src / compat / __init__.py
1 import sys
2
3 # sorted() was introduced in 2.4
4 if sys.version_info[:2] < (2, 4):
5     # Define our own version of sorted
6     def sorted(iterable, cmp=cmp, key=lambda x: x, reverse = False):
7         # Make a list out of the iterable
8         lst = list(iterable)
9         # Sort the list, using the given comparator and key function
10         lst.sort(lambda x, y: cmp(key(x), key(y)))
11         # Reverse if requested
12         if reverse:
13             lst.reverse()
14         # Done!
15         return lst
16 else:
17     sorted = sorted