huskies1313 huskies1313 - 7 months ago 12
Python Question

Alphabetizing a list through class methods

I have been trying to print a list using a

__str__
method of a class, but before the list can be printed it has to be alphabetized through other methods coming before the
__str__
method in the class. The provided methods for this are:

def returnName(self):
'''return the name of the player first last'''

return(self.first + self.last)

def returnReverseName(self):
'''return the name of the player as last, first'''
reverseName = (str(self.last), str(self.first))
return(reverseName)

def __eq__ (self, other):
'''determine if this persons name is the same as the other personsname'''

if self.returnReverseName == other.returnReverseName:
return True
else:
return False


def __lt__(self,other):
'''determine if this persons name is less than the other persons name alphabetically'''

if str(self.returnReverseName) < str(other.returnReverseName):
return True
else:
return False

def __gt__ (self, other):
'''determine if this persons name is greater than the other persons name alphabetically'''

if self.returnReverseName > other.returnReverseName:
return True
else:
return False

def __str__(self):
'''return a string of the persons name and their rating in a nice format'''

Name1 = str(self.first) + ' ' + str(self.last)
return ('%-20s %10.2f' % (Name1, self.rating))


where the name needs to be sorted by last name, first name but printed in first name last name order. The methods
__lt__
and
__gt__
are what should be ordering them when printed. The code I have so far to compile the list is:

basicList.sort()
for line in playerDict:
print(playerDict[line])


but this doesn't utilize the list, and is only printing the dictionary line by line in an unordered manner. Although it does refer to the methods it is just unable to alphabetize the names. How do I properly use the methods to get the dictionary printed in the right order? If I need to use the list of player objects, how do I get it to print using the
__str__
method that would be printed post-alphabetization?

Answer

This code has many mistakes and oddities, but the first thing it does that could actually cause wrong results is that __eq__ and __gt__ are comparing two functions rather than two names. __lt__, for some reason, compares the string representations of those two functions. I don't know why it has different behavior than the other functions, but it's still wrong (just in a different way).

And I'm not surprised that the sorted list isn't used - you never use it.

basicList.sort() # sort the list
for line in playerDict: # that's not basicList
    print(playerDict[line]) # still no basicList

Your class methods, if repaired, might look like this:

def returnName(self):
    '''return the name of the player first last'''
    return(self.first + ' ' + self.last)

def returnReverseName(self):
    '''return the name of the player as last, first'''
    return self.last, self.first

def __eq__ (self, other):
    '''determine if this persons name is the same as the other personsname'''
    return self.returnReverseName() == other.returnReverseName()

def __lt__(self,other):
    '''determine if this persons name is less than the other persons name alphabetically'''
    return self.returnReverseName() < other.returnReverseName()

def __gt__ (self, other):
    '''determine if this persons name is greater than the other persons name alphabetically'''
    return self.returnReverseName() > other.returnReverseName()

def __str__(self):
    '''return a string of the persons name and their rating in a nice format'''
    return '%-20s %10.2f' % (self.returnName(), self.rating)

But you probably don't need any of that. It looks like playerDict is a dictionary that holds some kind of keys attached to values that are Player objects (I assume that's the class name). Just iterate over those values and use a given sort key:

for entry in sorted(playerDict.values(), key=lambda x: x.last, x.first):
    print(entry)