python sort with comparator

66

python sort with comparator -

def mycmp(a, b):
	if a < b:
		return -1
	elif a > b:
		return 1
	return 0
sorted(lst, key=functools.cmp_to_key(mycmp))
# functools.cmp_to_key converts the mycmp to a "key function",
# which returns an object that can be sorted accoding to mycmp

Comments

Submit
0 Comments