How should I do custom sort in Python 3?
Posted
by S.Mark
on Stack Overflow
See other posts from Stack Overflow
or by S.Mark
Published on 2010-03-28T05:04:35Z
Indexed on
2010/03/28
5:13 UTC
Read the original article
Hit count: 335
In Python 2.x, I could pass custom function to sorted and .sort functions
>>> x=['kar','htar','har','ar']
>>>
>>> sorted(x)
['ar', 'har', 'htar', 'kar']
>>>
>>> sorted(x,cmp=customsort)
['kar', 'htar', 'har', 'ar']
Because, in My language, consonents are comes with this order
"k","kh",....,"ht",..."h",...,"a"
But In Python 3.x, looks like I could not pass cmp
keyword
>>> sorted(x,cmp=customsort)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'cmp' is an invalid keyword argument for this function
Is there any alternatives or should I write my own sorted function too?
© Stack Overflow or respective owner