I have the following object that I would like to keep in a container that is sorted on insertion and does not contain duplicates, so I am using a SortedSet
from sortedcontainers import SortedSet, SortedList
class R():
def __hash__(self):
return hash(self.person_id)
def __eq__(self, other):
return self.__class__ == other.__class__ and self.person_id == other.person_id
def __nq__(self, other):
return not (self == other)
def __lt__(self, other):
return other.value < self.value
def __init__(self, person_id, value):
self.person_id = person_id
self.value = value
def __repr__(self):
return "person: %s (%s)" % (self.person_id, self.value)
x = SortedSet()
x.add(R(13, 2))
x.add(R(17, 4))
x.add(R(11, 21))
x.add(R(7, -41))
print(x)
When I run this code I get the following output as expected:
SortedSet([person: 11 (21), person: 17 (4), person: 13 (2), person: 7 (-41)])
However if I added an extra duplicate element i.e. 17:
x.add(R(13, 2))
x.add(R(17, 4))
x.add(R(11, 21))
x.add(R(7, -41))
x.add(R(17, -67))
print(x)
I expect the R object with id 17 named person: 17 (4) to be moved to the back with value person: 17 (-67) like:
SortedSet([person: 11 (21), person: 13 (2), person: 7 (-41), person: 17 (-67)])
However nothing changes:
SortedSet([person: 11 (21), person: 17 (4), person: 13 (2), person: 7 (-41)])
How can I achieve the desired output as described using a SortedSet or any other container that is sorted on insertion and has no duplicates?