1

I'm having trouble sorting a list of objects based off of their count (an integer) and their name (a string). The output I desire is sorting first primarily by count, and then if there is a tie in count, to sort alphabetically by name.

I currently have a list of Person objects that have the attributes ._name and ._count.
My str method is :

def __str__(self):
    return str(self._name) + ' : ' + str(self._count)

If I print them out it comes out as:

David : 10
Steve : 5
Josh : 5
Anthony:5

and my desired output is:

David : 10
Anthony : 5
Josh : 5
Steve : 5

Is there anyway I can sort them alphabetically while keeping them in the correct order based on their ._count?

ARol101
  • 63
  • 5
  • Another duplicate [Can I sort a list of objects by 2-keys](https://stackoverflow.com/questions/29678659/can-i-sort-a-list-of-objects-by-2-keys) – Sohaib Farooqi Apr 19 '18 at 02:01

0 Answers0