2

I want to Sort this Arraylist

public List<Machine> machines;

machines = machineController.execute().get();
Collections.sort(machines, new Comparator<Machine>() {
    public int compare(Machine obj1, Machine obj2)
    {
        return obj1.getManufacturer().compareToIgnoreCase(obj2.getManufacturer());
    }
});

Now the List shows me the Manufacturer from A to Z. That works fine.

BMW Z1,

BMX X6,

Volkswagen Jetta,

Volkswagen Golf,

The Problem is: How can I sort by Manufacturer & Model? (obj.getManufaturer & obj.getModel)

BMW X6,

BMW Z1,

Volkswagen Golf,

Volkswagen Jetta

2 Answers 2

4

You would need to override "compare" in your Machine class to sort by both the Manufacturer and model. If you look at the way you have it now, you are simply comparing by Manufacturer. You want to compare by "Machine" as a whole entity.

That should be enough to solve your problem, but if you would like example code let me know and I can edit this post :)

EDIT: working example that I tested:

Inside of your Machine class use:

public class Machine implements Comparable<Machine>

Then, you need:

@Override
public int compareTo(Machine o) {
    if (manufacturer.compareToIgnoreCase(o.manufacturer) == 0)
        return model.compareToIgnoreCase(o.model);
    else
        return manufacturer.compareToIgnoreCase(o.manufacturer);
}

and then, you can simply call:

Collections.sort(machines);

Happy coding!

Here is the exact code I personally used for testing:

public class Machine implements Comparable<Machine> {

private String manufacturer;
private String model;
private double price;

public Machine(String man, String mod) {
    this(man, mod, 0);
}

public Machine(String man, String mod, double p) {
    manufacturer = man;
    model = mod;
    price = p;
}

/**
 * @return the manufacturer
 */
public String getManufacturer() {
    return manufacturer;
}

/**
 * @return the model
 */
public String getModel() {
    return model;
}

public double getPrice() {
    return price;
}

@Override
public int compareTo(Machine o) {
    if (manufacturer.compareToIgnoreCase(o.manufacturer) == 0)
        return model.compareToIgnoreCase(o.model);
    else
        return manufacturer.compareToIgnoreCase(o.manufacturer);
}

@Override
public String toString() {
    return manufacturer.toString() + " " + model.toString();
}

}

and

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Blah {
    public static void main(String[] args) {
        List<Machine> machines = new ArrayList<>();

        machines.add(new Machine("Volkswagen", "Jetta"));
        machines.add(new Machine("Volkswagen", "Golf"));
        machines.add(new Machine("BMW", "Z1"));
        machines.add(new Machine("BMW", "X6"));

        Collections.sort(machines);

        for (Machine m : machines)
            System.out.println(m.toString());
    }
}

For me, this outputs:

BMW X6
BMW Z1
Volkswagen Golf
Volkswagen Jetta
Sign up to request clarification or add additional context in comments.

7 Comments

Alright, give me just a second and I'll have the code snippets for you!
Thanks for help, but the List is unsorted now and no error is given.... Should i post more?
Try again, I had a typo somewhere. I ran this code on my machine and it works. Specifically, in the "compareTo" i wrote "return manufacturer.compareToIgnoreCase(manufacturer)" forgetting to say "o.manufacturer". Add that and it should work perfectly!
Thanks...Now it sorts only by Manufacturer .... but not by Manufaturer & Model... :/ java.lang.RuntimeException: Unable to start activity ComponentInfo{com.raso/com.am.raso.view.MachinesActivity}: java.lang.NullPointerException
@delive Assuming you're talking about this same example but sorting by two Objects, then yes you could. The specifics of how they are compared would be up to the compare implementation in the individual objects, and wouldn't be of concern to Machine here.
|
1

You can do this:

public int compare(Machine obj1, Machine obj2){
    if (obj1.getManufacturer().compareToIgnoreCase(obj2.getManufacturer()) != 0) {
        return obj1.getManufacturer().compareToIgnoreCase(obj2.getManufacturer());
    } else {
        return obj1.getModel().compareToIgnoreCase(obj2.getModel());
    }
}

1 Comment

it should work. what I do here is compare manufacturer and see what value that is. if it is not zero it means they are different so it returns that, else it returns the compare of the model because it means manufacterer is the same

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.