Sorting: Comparator

  • + 0 comments
    class Player:
        def __init__(self, name, score):
            self.name = name
            self.score = score
            
        def __repr__(self):
            return self.name,self.score
            
        def comparator(a, b):
            if a.score == b.score and a.name == b.name:
                return 0
            if a.score > b.score:
                return -1
            elif b.score > a.score:
                return 1
            if a.name < b.name:
                return -1
            return 1