python - Sort with two key value -
i'm trying convert code, , ran line, , i'm not sure does. know can sort using key a:a*a
, how handle 2 keys?
this line trying convert (to c#, if matters):
arr.sort(lambda a,b:a-b)
https://docs.python.org/2/tutorial/datastructures.html
list.sort(cmp=none, key=none, reverse=false)
sort items of list in place (the arguments can used sort customization, see sorted() explanation).
https://docs.python.org/2/library/functions.html#sorted
cmp specifies custom comparison function of 2 arguments (iterable elements) should return negative, 0 or positive number depending on whether first argument considered smaller than, equal to, or larger second argument: cmp=lambda x,y: cmp(x.lower(), y.lower()). default value none.
lambda a,b
defining anonymous function in python.
so same
def f(a,b): return a-b arr.sort(f)
which naturally returns > 0
if a > b
, 0
if a==b
.
as @martijnpieters points out, should plain "natural" sorting and, in absence of operator overloading (e.g. plain integers), same sort()
no arguments.
Comments
Post a Comment