Scott David Daniels wrote: > if sys.version_info < (2, 4): > def sorted(iterable, cmp=None, key=None, reverse=False): > "return a sorted copy of its input" > seq = list(iterable) > if reverse: > seq.reverse() # preserve stability > if key is not None: > seq = [(key(elem), i, elem) for i, elem > in enumerate(seq)] > seq.sort(cmp) > if key is not None: > seq = [elem for (key, i, elem) in seq] > if reverse: > seq.reverse() > return seq
I think you may have some unintended indentation on the 'seq.sort' line otherwise this only sorts when a key is specified. Also, you probably want: if key is not None: if reverse: seq = [(key(elem), -i, elem) for i, elem in enumerate(seq)] else: seq = [(key(elem), i, elem) for i, elem in enumerate(seq)] to handle the case where both key and reverse are given. -- http://mail.python.org/mailman/listinfo/python-list