Created
January 15, 2019 02:44
-
-
Save collinanderson/d25c8fe5e679748b6d47d4475e87cb91 to your computer and use it in GitHub Desktop.
Python 2 sorting in python3
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
def default_3way_compare(v, w): # Yes, this is how Python 2 sorted things :) | |
tv, tw = type(v), type(w) | |
if tv is tw: | |
return -1 if id(v) < id(w) else (1 if id(v) > id(w) else 0) | |
if v is None: | |
return -1 | |
if w is None: | |
return 1 | |
if isinstance(v, (int, float)): | |
vname = '' | |
else: | |
vname = type(v).__name__ | |
if isinstance(w, (int, float)): | |
wname = '' | |
else: | |
wname = type(w).__name__ | |
if vname < wname: | |
return -1 | |
if vname > wname: | |
return 1 | |
return -1 if id(type(v)) < id(type(w)) else 1 | |
def py2key(func=None): # based on cmp_to_key | |
class K(object): | |
__slots__ = ['obj'] | |
__hash__ = None | |
def __init__(self, obj): | |
self.obj = func(obj) if func else obj | |
def __lt__(self, other): | |
try: | |
return self.obj < other.obj | |
except TypeError: | |
return default_3way_compare(self.obj, other.obj) < 0 | |
def __gt__(self, other): | |
try: | |
return self.obj > other.obj | |
except TypeError: | |
return default_3way_compare(self.obj, other.obj) > 0 | |
def __eq__(self, other): | |
try: | |
return self.obj == other.obj | |
except TypeError: | |
return default_3way_compare(self.obj, other.obj) == 0 | |
def __le__(self, other): | |
try: | |
return self.obj <= other.obj | |
except TypeError: | |
return default_3way_compare(self.obj, other.obj) <= 0 | |
def __ge__(self, other): | |
try: | |
return self.obj >= other.obj | |
except TypeError: | |
return default_3way_compare(self.obj, other.obj) >= 0 | |
return K |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment