Daniel Jürgens
Daniel Jürgens

Reputation: 21

How to use bubblesort with instances

Good evening guys,

for my homework i have to implement a class called 'car' which consists of certain parameters like 'registration', 'color', 'manufacturer', 'model' with with output and comparison methods. After this i have to implement a bubble sort to sort these instances by 'manufacturer' and 'model'; the other attributes are neglectable.

my Code so far:

def class car():
    self.__init__(self, registration, color, manufacturer, model):
        self.registration = registration
        self.color = color
        self.manufacturer = manufacturer
        self.model = model
    def output(self):
        return '{}{}{}{}'.format(self.registration, self.color, self. manufacturer, self.model)

    def bubblesort(list_1):
        for k in range(len(list_1)-1, 0, -1):
            for i in range(0,k):
                if list_1[i] >  list[i+1]:
                    list_1[i], list_1[i+1] = list_1[i+1], list_1[i]
        return list_1

what i have to do now is two create an instance of car like this:

instance = [Car(2003, 'black', 'BMW', 'M4')
            Car(2005, 'red', 'Audi', 'Q3')
            Car(2010, 'green', 'BMW', 'X1')
            Car(2007, 'pink', 'Subaru', 'BRZ')]
            Car(1998, 'black', 'Audi', 'Q5')

and after sorting it has to look like this:

[Car(2005, 'red', 'Audi', 'Q3')
Car(1998, 'black', 'Audi', 'Q5')
Car(2003, 'black', 'BMW', 'M4')
Car(2010, 'green', 'BMW', 'X1')
Car(2007, 'pink', 'Subaru', 'BRZ')]

So I have pretty much everything but i don't know how to sort 'the whole instance' - actually i don't even know if you can say this...:D

Maybe some of you guys can help me out; thanks.

Upvotes: 0

Views: 916

Answers (2)

aldarel
aldarel

Reputation: 456

Here is the sample code that does what you need. The comparison operator is overloaded (I guessed the logic of comparison).

class Car(object):
    def __init__(self, registration, color, manufacturer, model):
        self.registration = registration
        self.color = color
        self.manufacturer = manufacturer
        self.model = model

    def __str__(self):
        return '{}\t{}\t{}\t{}'.format(self.registration, self.color, self. manufacturer, self.model)

    def __gt__(self, other):
        if isinstance(other, Car):
            if self.manufacturer != other.manufacturer:
                return self.manufacturer.lower() > other.manufacturer.lower()
            else:
                return self.model.lower() > other.model.lower()

def bubblesort(list_1):
    for k in range(len(list_1)-1, 0, -1):
        for i in range(0,k):
            if list_1[i] >  list_1[i+1]:
                list_1[i], list_1[i+1] = list_1[i+1], list_1[i]
    return list_1

instance = [Car(2003, 'black', 'BMW', 'M4'),
            Car(2005, 'red', 'Audi', 'Q3'),
            Car(2010, 'green', 'BMW', 'X1'),
            Car(2007, 'pink', 'Subaru', 'BRZ'),
            Car(1998, 'black', 'Audi', 'Q5')]

sorted_instance = bubblesort(instance)
for item in sorted_instance:
    print(str(item))

Upvotes: 1

9000
9000

Reputation: 40894

You can only sort comparable things, that is, something that can be thought of "less than" another such thing.

For cars it might be model name, year, etc, or a combination of these.

To make an object comparable, you need to implement methods like __lt__ (less tan) and __eq__ (equals).

But your class seems to be just a bunch of immutable fields (which is usually good). Try using a namedtuple, it creates a class for you, and it provides an implementation for comparison methods. With it, your cars would be sorted by the first field declared, then by second, etc.

Also, the bubblesort method definitely does not belong to the Car class, for it never uses any instance data of a car. It should be a standalone function.

Upvotes: 0

Related Questions