# # 14 - Data Structures: Sorting! Renato

## # Goals

• learn how to sort Data structures
• learn the differences between Comparable and Comparator
• what is static?
• Practice!

## # Bike Class

``````package lesson14;

public class Bike implements Comparable<Bike>{

private String model;
private int price;
private int wheelSize;

public Bike(String model, int price, int wheelSize) {
this.model = model;
this.price = price;
this.wheelSize = wheelSize;
}

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}

public int getPrice() {
return price;
}

public void setPrice(int price) {
this.price = price;
}

public int getWheelSize() {
return wheelSize;
}

public void setWheelSize(int wheelSize) {
this.wheelSize = wheelSize;
}

@Override
public int compareTo(Bike bike) {

// this > bike +
if (this.getPrice() > bike.getPrice())
return 1;
else
return -1;
}
}
``````

sort an ArrayList of Bikes

``````public class Main {

public static void main(String [] args) {

List<Bike> bikes = new ArrayList<>();

for (Bike b : bikes) {
System.out.println(b.getPrice() + " " + b.getModel());
}

// we now sort the bikes

Collections.sort(bikes);

for (Bike b : bikes) {
System.out.println(b.getPrice() + " " + b.getModel());
}

}
}
``````

## # Exercises

### # Exercise 1

Let's now sort the bikes by brand DESCENDING. How do you do that?

### # Exercise 2

Last Updated: 10/16/2019, 2:32:54 PM