简体   繁体   中英

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

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;
    }
}

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 Color s by the order field:

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

First rename your Fruit.compareTo method to something else, because it's not a compareTo method. It returns a "sort key" - something that's used for sorting - so let's call it getSortKey for now:

// @Override REMOVE THIS
public int getSortKey(/* Fruit fruit REMOVE THIS */) {
    if (this.getColor().getName().equals("white")){
        return 0;
    }
    if (this.getColor().getName().equals("yellow")){
        return 1;
    }
    ...

Now, FruitSortingMachine can sort your Fruits by comparing the sort keys:

fruits.sort(Comparator.comparing(Fruit::getSortKey));

Another (somewhat older) way of doing this is implementing Fruit.compareTo , which is easy now that you have the getSortKey method:

@Override
public int compareTo(Fruit that) {
     return Integer.compare(this.getSortKey(), that.getSortKey());
}

If you do that, you can sort using Collections.sort

Collections.sort(fruits)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM