Sort list of python objects

1

I'm trying to sort a list of Player objects in Django1.10 and Python 3.5. But it is impossible for me and I can not find the error.

The object class is the following

class Jugador(models.Model):

#Otras propiedades
rating = models.IntegerField(null=True)

def __cmp__(self, other):
    if self.rating < other.rating:
        rst = -1
    elif self.rating > other.rating:
        rst = 1
    else:
        rst = 0

    return rst

To sort the list, after calculating the rating of each player, I save the property and add it to a list, which I would like it to be sorted by the rating property.

def ratingCalculte(jugadores, user, temporada):
result = []
#Calculando rating
.....
.....

    jugador.rating = rating
    jugador.save()
    result.append(jugador)

sorted(result)

return result

The exception is:

unorderable types: Jugador() < Jugador()

I have tried everything, and I do not know what it can do. And previously in a beta version of the application I ordered in the same way and there was no problem.

    
asked by kylehide65 17.04.2017 в 16:19
source

1 answer

0

If you are using Python3, defining the Cmp method does not make the object "dodgeable", it is better to define the comparisons that should be made, for a simple sort it would be enough to define the method __lt__() or "less than".

    
answered by 17.04.2017 / 18:35
source