Created
July 1, 2020 16:53
-
-
Save sansyrox/c4419197bd1d37ec1aff6ce2a3d089bb to your computer and use it in GitHub Desktop.
Custom Comparator sort in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import cmp_to_key | |
sorted(mylist, key=cmp_to_key(compare)) | |
def compare(item1, item2): | |
if fitness(item1) < fitness(item2): | |
return -1 | |
elif fitness(item1) > fitness(item2): | |
return 1 | |
else: | |
return 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment