'''
Demonstrates an apparent jython bug. If the __cmp__ method and the __eq__ method use different criteria
for equality (generally a bad idea, admittedly), the sorted results follow neither criterion, but
appear to be a mix of the two.
Under python these sort as intended, like:
[1-03, 1-04, 1-06, 1-06, 1-07, 1-08, 1-08, 2-01, 2-01, 2-02, 2-02, 2-03, 2-07, 2-08, 2-08, 2-08, 3-04, 3-05, 3-07, 3-08, 3-08, 3-08, 3-09, 3-09, 3-10]
Under jython they sort in an apparently nonsensical way:
[1-02, 1-03, 1-06, 1-07, 1-08, 1-08, 1-09, 1-09, 2-04, 2-10, 3-01, 3-03, 3-06, 3-06, 3-07, 3-08, 3-08, 2-08, 3-08, 1-08, 3-06, 1-06, 2-05, 3-09, 2-09]
'''
from random import randint
class tricky_object:
def __init__(self):
self.keyfield1 = "%1d" % (randint(1,3))
self.keyfield2 = "%02d" % (randint(0,10))
def __cmp__(self,other):
if self.keyfield1 != other.keyfield1:
return cmp(self.keyfield1,other.keyfield1)
return cmp(self.keyfield2,other.keyfield2)
def __eq__(self,other):
return (self.keyfield2 == other.keyfield2)
def __repr__(self):
return "%s-%s" %(self.keyfield1,self.keyfield2)
tos = [tricky_object() for i in range(25)]
tos.sort()
print tos |