During a current school project of mine, I came up with a specific sorting algorithm which I nicknamed parallel_sort. By giving it a list of integers and a list of dictionaries, it sorts the dictionaries according to the integers. For example :
parallel_sort([1,0,2],[{1:1},{0:0},{2:2}]) #[{0:0},{1:1},{2:2}]
It works perfectly fine, and I wondered if I could make it simpler by using the sort method. I first thought of using a list of (integer,dictionary) tuples, but in some cases the integers were the same, so I couldn't. So I had an idea : if I had a class that couldn't be compared to anything and put it the middle of those tuples, it could stop the tuple comparison to consider the dictionaries. The new parallel_sort would work in the following way :
def parallel_sort(int_lst,dict_lst):
tuple_lst=[]
for i in range(len(int_lst)):
tuple_lst.append((int_lst[i],incomparable_class,dict_lst[i]))
tuple_lst.sort()
res=[]
for tupl in tuple_lst:
res.append(tupl[2])
return res
Moreover, I realized that as long as the incomparable class stopped the tuple comparison, I could even put mixed types behind it, meaning I could do stuff like this :
parallel_sort([1,0,2],[0,'0',[]]) #['0',0,[]]
So I tried to make said class :
class Incomparable:
def __init__(self): #The incomparable class itself doesn't do anything
pass
def __eq__(self,obj): #Never equal to anything
return False
def __ne__(self,obj):
return True
def __gt__(self,obj): #Never greater than anything
return False
def __ge__(self,obj):
return False
def __lt__(self,obj): #Never lesser than anything
return False
def __le__(self,obj):
return False
I obviously did some testing, and then ran into an issue :
incmp=Incomparable()
0<incmp
#False
0>incmp
#False
0==incmp
#False
incmp==incmp
#False
incmp!=incmp
#True
(0,incmp,0)<(0,0,'0')
#True
(0,incmp,0)>(0,0,'0')
#True
#Up to here, it works as intended
(0,incmp,0)<(0,incmp,'0')
#TypeError: '>' not supported between instances of 'int' and 'str'
I tried to find out if tuple comparison had additional rules for classes, but there didn't seem to be. I knew it works based on the lexicographic order, and I thought I had successfully bypassed that with my incomparable class. Since incmp!=incmp is True, the comparison was supposed to stop.
Even by puting True and False in different ways in the incomparable class definition, it never works.
What goes wrong during the tuple comparison ? Is there some way to make the incomparable class work, or to have something that would work along the same line ?