mnba / shedskin

Automatically exported from code.google.com/p/shedskin
0 stars 0 forks source link

implement the sort semantics of class obj #148

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
implement the sort semantics of class obj through the __lt__ method.
It's quite common to sort complex obj list

test case:

class inst(object):
    def __init__(self, num, opcode='add', pc='1'):
        self.opcode = opcode
        self.pc = pc
        self.num = num

    def __lt__( self, other):
        return self.num < other.num

    def __repr__(self): 
        return "%d" % self.num

Seq = [inst(3),inst(1),inst(4),inst(2)]
print Seq
print sorted(Seq)

Python output:
[3, 1, 4, 2]
[1, 2, 3, 4]

Original issue reported on code.google.com by jason.mi...@gmail.com on 16 Jul 2011 at 8:28

GoogleCodeExporter commented 9 years ago

Original comment by mark.duf...@gmail.com on 16 Jul 2011 at 10:49