Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm pretty new to java and don't really know about these stuff and it's my first time writing these methods! of course I have no clue how I can write them.

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 statemnet .compareto() The operator && is undefined for the argument type(s) java.lang.String, java.lang.String

share|improve this question

closed as off topic by Corbin, dreza, Brian Reichle, Trevor Pilley, svick Nov 17 '12 at 11:54

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
umm, this question is probably best asked on SO if you are looking for help on why it's not working. Otherwise have you tried + instead of && –  dreza Nov 17 '12 at 6:17
    
What are you trying to do? It's really not clear. –  Nick ODell Nov 17 '12 at 6:26
    
@dreza what do you think about this: if(vehicles[x].getMake().compareTo(vehicles[x + 1].getMake()) > 0 && vehicles[x].getModel().compareTo(vehicles[x + 1].getModel()) > 0 ) { –  Niloo Rad Nov 17 '12 at 6:38
    
@NilooRad seems a better approach. I assume that at least it doesn't error now. If so perhaps you might like to adjust your question for getting assistance with actually reviewing the code... –  dreza Nov 17 '12 at 6:50
add comment

1 Answer

up vote 1 down vote accepted

There are probably better approaches as my knowledge Java is a bit limited but have you considered the Array.sort implementation?

Using that you might do something like:

public void sortByVehicleMakeModel(Vehicle[] vehicles) {  
    Arrays.sort(vehicles, new VehicleComparator());
}

class VehicleComparator implements Comparator<Vehicle> {

    public int compare(Vehicle v1, Vehicle v2) {
         return getMakeModel(v1).compareTo(getMakeModel(v2));
    }

    private String getMakeModel(Vehicle v) {
        return v.getMake() + v.getModel();
    }
}
share|improve this answer
    
nice one thank you :) –  Niloo Rad Nov 17 '12 at 7:30
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.