Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Quick Python sort question!

  • 19-12-2007 02:32PM
    #1
    Registered Users, Registered Users 2 Posts: 12,051 ✭✭✭✭


    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, Registered Users 2 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, Registered Users 2 Posts: 12,051 ✭✭✭✭L'prof


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


  • Registered Users, Registered Users 2 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, Registered Users 2 Posts: 12,051 ✭✭✭✭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, Registered Users 2 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, Registered Users 2 Posts: 12,051 ✭✭✭✭L'prof


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


Advertisement