码迷,mamicode.com
首页 > 编程语言 > 详细

Python 多级排序

时间:2014-09-12 13:35:03      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:python


class C( object ):
    
    def __init__( self, x1, x2, x3 ):
        self.x1 = x1
        self.x2 = x2
        self.x3 = x3


    def __cmp__( self, other ):
        if self.x1 < other.x1:
            return -1
        elif self.x1 == other.x1:
            if self.x2 < other.x2:
                return -1
            elif self.x2 == other.x2:
                if self.x3 < other.x3:
                    return -1
                elif self.x3 == other.x3:
                    return 0
                else:
                    return 1
            else:
                return 1
        else:
            return 1


    def __repr__( self ):
        return "({x1}, {x2}, {x3})".format( x1 = self.x1,
                                            x2 = self.x2,
                                            x3 = self.x3 )
        

import random

r = random.randint

li = []
for i in xrange( 15 ):
    li.append( C( r( 1, 10 ), r( 1, 10 ), r( 1, 10 ) ) )

for i in li:
    print i

print 

li1 = sorted( li )
for i in li1:
    print i


Python 多级排序

标签:python

原文地址:http://blog.csdn.net/pandora_madara/article/details/39228147

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!