0

I'm learning how to use Classes, so far I have achieved the following:

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)

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.

Enigmatic
  • 3,902
  • 6
  • 26
  • 48
  • Where do you wish to have this function? It cannot be in class. It will be outside this class. This function will take as input an array of customer. And the algorithm will run over the money property of customer. What is the exact issue in creating the code? Do you wish to use pre-built sorting methods? – prabodhprakash Sep 26 '16 at 09:34
  • You'll be sorting a *iterable/container*, but you don't seem to have one yet – Moses Koledoye Sep 26 '16 at 09:35
  • @prabodhprakash That's exactly what I'm unsure about. – Enigmatic Sep 26 '16 at 09:36
  • @MosesKoledoye How would I go ahead and do that? – Enigmatic Sep 26 '16 at 09:36
  • @ThatOnePythonNoob You have to put your customer objects in a list first to have a sortable order. See my answer below. list_name = [list_obj1, list_obj2,...] creates a list of objects in the variable list_name – Igl3 Sep 26 '16 at 09:47

2 Answers2

3

You can sort a list of objects in place by an attribute like this:

your_list.sort(key=lambda x: x.attribute_name, reverse=True)

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

So in your case:

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]

For more information check this question too

Community
  • 1
  • 1
Igl3
  • 4,900
  • 5
  • 35
  • 69
-1
def sort_by_money(customer)
    for index in range(1,len(customer)):
        currentvalue = customer[index].money
        position = index

        while position>0 and customer[position-1].money > currentvalue:
            alist[position]=alist[position-1]
            position = position-1

        customer[position]=customer

Simple insertion sort that takes in customer array and sorts it back based on money.

This code will be outside your customer class that will take customer array as input.

There can be many correct answer to this problem. Written insertion sort to explain properly.

prabodhprakash
  • 3,825
  • 24
  • 48
  • He hasn't got a list of customers yet. Furthermore there is no need of implementing a sort function by himself, as there are a lot of performant methods already available for iterables in python. – Igl3 Sep 26 '16 at 09:48
  • @Igle - I have specifically mentioned in my answer that it is for the purpose of explanation that I have written insertion sort. If you read my comments in the question, I have mentioned about using pre-built methods. – prabodhprakash Sep 26 '16 at 09:51
  • @timgeb While your eyes might be hurt, the question does not care about it. The person asking question is pretty new to it and the entire purpose of writing it this way was to ensure that it is as elaborate as possible ! – prabodhprakash Sep 26 '16 at 09:52
  • @prabodhprakash you are teaching bad python by iterating the way you do, so it's very much relevant. There's barely ever a reason to write `for index in range(1,len(customer))`. – timgeb Sep 26 '16 at 09:53
  • He was not asking for sorting algorithm implementations, but just for a function, that is able to sort a sequence of customers. Providing bad code as example doesn't help to explain properly. – Igl3 Sep 26 '16 at 09:53
  • @Igle "@prabodhprakash That's exactly what I'm unsure about." The person asking question is pretty unsure about it. I'm not sure, how can you be so confident about this ! – prabodhprakash Sep 26 '16 at 09:55
  • @timgeb - I can partially agree to you. – prabodhprakash Sep 26 '16 at 09:55