Will you check my Method and let me know what I’m doing wrong? thank you 🙂
public static void sortByVehicleMakeModel(Vehicle[] vehicles) {
boolean swapped = true;
for(int y = 0; y < vehicles.length && swapped; y++) {
swapped=false;
for(int x = 0; x < vehicles.length - (y+1); x++) {
if(vehicles[x].getMake() && vehicles[x].getModel().compareTo(vehicles[x + 1].getMake() && vehicles[x].getModel())) {
swap(vehicles, x, x + 1);
swapped=true;
}
}
}
}
my error is on the second statement .compareto()
The operator && is undefined for the argument type(s) java.lang.String, java.lang.String
However, this code works just fine:
public static void sortByOwnerName(Vehicle[] vehicles) {
boolean swapped = true;
for(int y = 0; y < vehicles.length && swapped; y++) {
swapped=false;
for(int x = 0; x < vehicles.length - (y + 1); x++) {
if(vehicles[x].getOwner().getName().compareTo(vehicles[x + 1].getOwner().getName())> 0) {
swap(vehicles, x, x + 1);
swapped=true;
}
}
}
}
I would suggest adding a
int getCost()to the Vehicle object and then using something likevehicles[x].getCost() > vehicles[x - 1].getCost()for your if statement.Also, this sort is not very efficient. Maybe Vehicle should implement Comparable and use
Collections.sort()to sort.Just read the update to your question.
Try this: