How to sort list with compareTo method of specific order of colors in Java

Ronny Giezen :

I'm trying to make a compareTo method that I use to sort a list in the order of the following colors: white, yellow, orange, red, blue, purple, black. Where white is the first color.

I have an object Fruit which contains different types of fruit, these fruits are specified in extended classes. All fruits have a color (which also is in the list of colors).

I was trying to write an if-else statement so that if the color of the fruit is yellow return 1 etc. and that for all colors. But that didn't seem to work.

Can anybody help me to write the compareTo method?

Thanks in advance!

My abstract Fruit class where I implement the compareTo method:

public abstract class Fruit implements Comparable<Fruit>, Edible {

    String name;
    Color color;
    boolean fluid;


    public Fruit(String name, Color color, boolean fluid) {
        this.name = name;
        this.color = color;
        this.fluid = fluid;
    }

    public abstract boolean isRotten();



    @Override // I don't know how to create this one correctly
    public int compareTo(Fruit fruit) {
        if (this.getColor().getName().equals("white")){
        return 0;
    }
    if (this.getColor().getName().equals("yellow")){
        return 1;
    }
    if (this.getColor().getName().equals("orange")){
        return 2;
    }
    if (this.getColor().getName().equals("red")){
        return 3;
    }
    if (this.getColor().getName().equals("blue")){
        return 4;
    }
    if (this.getColor().getName().equals("purple")){
        return 5;
    }
    if (this.getColor().getName().equals("black")){
        return 6;
    }
    else return -1;

}

    public Color getColor() {
        return color;
    }

    @Override
    public boolean isEdible() {
        return !isRotten();
    }
}

The class for Color:

public class Color {


    private String name;

    public static final String WHITE = "white";
    public static final String YELLOW = "yellow";
    public static final String ORANGE = "orange";
    public static final String RED = "red";
    public static final String BLUE = "blue";
    public static final String PURPLE = "purple";
    public static final String BLACK = "black";


    public String getName() {
        return name;
    }

    public Color(String name) {
        this.name = name;
    }




}

The class where I add the different fruits to a list and sort the list:

public class FruitSortingMachine {

    private List<Fruit> fruits = new ArrayList<>();

    /**
     * Instantiates a new Fruit sorting machine.
     */
    public FruitSortingMachine(){}

    /**
     * Sort.
     */
    public void sort() {
        Collections.sort(fruits);
    }

    /**
     * Gets fruits.
     *
     * @return the fruits
     */
    public List<Fruit> getFruits() {

        return this.fruits;
    }

    /**
     * Add fruit boolean.
     *
     * @param fruit the fruit
     * @return the boolean
     */
    public boolean addFruit(Fruit fruit) {
        if (!fruit.isEdible()){
            return false;
        }
        else this.fruits.add(fruit);
        return true;
    }
}
Andronicus :

I'd turn Color into enum:

public enum Color {

    WHITE("white", 0),
    YELLOW("yellow", 1),
    ORANGE = "orange", 2),
    RED("red", 3),
    BLUE("blue", 4),
    PURPLE("purple", 5),
    BLACK("black", 6);

    private String name;
    private int order;

    // getters, setters etc.

}

Then you can compare the Colors by the order field:

@Override
public int compareTo(Fruit fruit) {
    return Integer.compare(this.getColor().getOrder(), fruit.getColor().getOrder());
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=371892&siteId=1