Skip to content

Instantly share code, notes, and snippets.

@maxwillzq
Created October 23, 2013 01:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxwillzq/7110909 to your computer and use it in GitHub Desktop.
Save maxwillzq/7110909 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 02 11:03:42 2013
@author: qzhang
"""
import functools
a = [ 1,50,2,4,16]
def MyCmp(x,y):
return cmp(y,x)
print sorted(a,cmp=cmp)
print sorted(a,cmp = MyCmp)
#make a key function using cmp_to_key()
get_key = functools.cmp_to_key(MyCmp)
for i in a:
print get_key(i).obj
#sort it by key, but not by cmp
print sorted(a,key=get_key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment