擅长:python、mysql、java
<p>可以按如下属性对在位对象列表进行排序:</p>
<pre><code>your_list.sort(key=lambda x: x.attribute_name, reverse=True)
</code></pre>
<p>如果设置<code>reverse=False</code>,则列表按升序排列,其中<code>reverse=True</code>按从最高到最低的顺序排列。你知道吗</p>
<p>所以在你的情况下:</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="https://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>