Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Quick Python sort question!

Options
  • 19-12-2007 2:32pm
    #1
    Registered Users Posts: 12,036 ✭✭✭✭


    I have a list of objects called teams, this is what each object looks like:
    class team:
        def __init__(self, name):
            self.name = name
            self.played = 0
            self.won = 0
            self.drawn = 0
            self.lost = 0
            self.g_for = 0
            self.g_against = 0
            self.g_difference = 0
            self.points = 0
    

    I know python has a sort function, which can be called by teams.sort() which seems to sort my list in some funny way (I didn't expect that to work btw). I want to sort my list into descending order according to the points each team has...is there a way I can use the sort() method to do this or do I have to come up with my own sorting algorithm for it?

    Thanks,
    Jay


Comments

  • Registered Users Posts: 246 ✭✭Takca


    Hi,
    You can pass in a sortfunction for sort to use when ordering your list,
    something like this should do the trick, just look up the docs on list.sort to
    see how it should work.

    def sf(a,b):
    	if a.points > b.points: return -1
    	if a.points < b.points: return 1
    	return 0
    


    then you can call

    teamlist.sort(sf)

    Hope this helps,
    Derek.


  • Registered Users Posts: 12,036 ✭✭✭✭L'prof


    Yeah, that works...thanks very much :D


  • Registered Users Posts: 304 ✭✭PhantomBeaker


    derekh wrote: »
    Hi,
    def sf(a,b):
    	if a.points > b.points: return -1
    	if a.points < b.points: return 1
    	return 0
    

    You can also add that function to the class, calling it __cmp__(self, other) and you don't have to pass in the function. Just list_of_teams.sort()

    It's rather nifty (and referred to as operator overloading - it's in the docs, you can do all kinds of funky things with it).

    Regards,
    Aoife


  • Registered Users Posts: 12,036 ✭✭✭✭L'prof


    You can also add that function to the class, calling it __cmp__(self, other) and you don't have to pass in the function. Just list_of_teams.sort()

    It's rather nifty (and referred to as operator overloading - it's in the docs, you can do all kinds of funky things with it).

    Regards,
    Aoife

    Thanks for that but, I'm not sure I really understand what you mean! Thanks anyway though!


  • Registered Users Posts: 219 ✭✭rjt


    jasonorr wrote: »
    Thanks for that but, I'm not sure I really understand what you mean! Thanks anyway though!

    For sort() to work, Python needs to be able to compare two team objects using <, > and ==. However, these comparisons are undefined for teams. In order to define it, we must define the __cmp__(self, other) method, which should return a negative number if self < other, 0 if self == other and a positive number if self > other.

    Eg. (stealing Derek's code):
    def __cmp__(self, other):
    	if self.points > other.points: return -1
    	if self.points < other.points: return 1
    	return 0
    

    Once you've done this you can simply call team_list.sort(). Note that the signs are reversed from what I said above. Want to guess why?

    Also, the above code could also be written as:
    def __cmp__(self, other):
    	return other.points - self.points
    
    Which is more concise (but probably less readable).

    Excuse any mistakes in the above, I'm not a Python user but sort() seems to work the same as it does in C++. For a better idea read:
    http://wiki.python.org/moin/HowTo/Sorting


  • Advertisement
  • Registered Users Posts: 12,036 ✭✭✭✭L'prof


    Ah right, I have you now...have that implemented and working...thanks RJT and Aoife!


Advertisement