Create a function that sorts customers of the bank by amount of money
<p>I'm learning how to use Classes, so far I have achieved the following:</p>

<pre><code>class customer:
    def __init__ (self, name, ID, money):
        self.name = name
        self.ID = ID
        self.money = money
    def deposit(self, amount):
        self.money = self.money+amount
    def withdraw(self, amount):
        self.money = self.money-amount

mike = customer('Mike', 1343, 1884883)
john = customer('John', 1343, 884839)
steve = customer('Steve', 1343, 99493)
adam = customer('Adam', 1343, 10000)
</code></pre>

<p>I would like to create a function that sorts the customers by the amount of money they have but am unsure about how to do so.</p>

<p>You can sort a list of objects in place by an attribute like this:</p>

<pre><code>your_list.sort(key=lambda x: x.attribute_name, reverse=True)
</code></pre>

<p>If you set <code>reverse=False</code>, the list is ordered ascending, with <code>reverse=True</code> it is sorted from highest amount to lowest.</p>

<p>So in your case:</p>

<pre><code>class customer:
    def __init__ (self, name, ID, money):
        self.name = name
        self.ID = ID
        self.money = money
    def deposit(self, amount):
        self.money = self.money+amount
    def withdraw(self, amount):
        self.money = self.money-amount


mike = customer('Mike', 1343, 1884883)
john = customer('John', 1343, 884839)
steve = customer('Steve', 1343, 99493)
adam = customer('Adam', 1343, 10000)

unsorted_list = [steve, adam, mike, john]

print [c.name for c in unsorted_list]

unsorted_list.sort(key=lambda c: c.money, reverse=True)

print [c.name for c in unsorted_list]
</code></pre>

<p><a href="http://stackoverflow.com/questions/403421/how-to-sort-a-list-of-objects-in-python-based-on-an-attribute-of-the-objects">For more information check this question too</a></p>

python python-2.7 function class initialization