-1

I have list of objects that looks like this

List<Color> colorList = generateColorList(10);

System.out.println(colorList);

System.out.println(colorList.stream().sorted().collect(Collectors.toList())); //Sorting

// [Color{colors=[#91E7CE, #FCEF32, #859BDF], isPrimary=true, rgb={r=126, b=155, g=112}}, 
// Color{colors=[#E53DE3, #589003, #12AC95], isPrimary=false, rgb={r=111, b=140, g=232}}, 
// Color{colors=[#90381C, #83E0A5, #D88C51], isPrimary=false, rgb={r=234, b=154, g=3}}, 
// Color{colors=[#D40BF0, #8625B4, #CA1F2A], isPrimary=false, rgb={r=198, b=231, g=18}}, 
// Color{colors=[#FB1203, #970BFA, #7FEA33], isPrimary=false, rgb={r=238, b=7, g=56}}, 
// Color{colors=[#68FB95, #34DBE8, #8953EF], isPrimary=true, rgb={r=121, b=171, g=167}}, 
// Color{colors=[#9D85F3, #1455AE, #374475], isPrimary=false, rgb={r=225, b=177, g=70}}, 
// Color{colors=[#43466A, #F3CD61, #3962FB], isPrimary=false, rgb={r=12, b=163, g=194}}, 
// Color{colors=[#311731, #8BF4FD, #40EF0C], isPrimary=true, rgb={r=89, b=80, g=153}}, 
// Color{colors=[#9015A9, #603793, #62F78B], isPrimary=true, rgb={r=111, b=253, g=88}}]

It has three datatypes that are listed in my class below

I want to sort them by isPrimary field

Color.class

public class Color implements Serializable, Comparable {
    private List<String> colors;
    @JsonProperty("isPrimary")
    private boolean primary;
    private HashMap<String,Integer> rgb = new HashMap<String,Integer>();
    public Color(){};

public List<String> getColors() {
    return colors;
}

public void setColors(List<String> colors) {
    this.colors = colors;
}

public boolean GetIsPrimary() {
    return primary;
}

public void setIsPrimary(boolean primary) {
    this.primary = primary;
}

public HashMap<String, Integer> getRgb() {
    return rgb;
}

public void setRgb(HashMap<String, Integer> rgb) {
    this.rgb = rgb;
}

@Override
public String toString() {
    return "Color{" +
            "colors=" + colors +
            ", isPrimary=" + primary +
            ", rgb=" + rgb +
            '}';
}

@Override
public int compareTo(Object o) {
    Color o1 = (Color)this;
    Color o2 = (Color)o;
    return o1.GetIsPrimary().compareTo(o2.GetIsPrimary());
}

}

I was trying to repeat after my tutor but he had String and i have boolean and it tells me that method compareTo is not resolved(the last method in Color class) How can i make it working?

1 Answers1

1

You should do the following:

  • Comparable is a generic interface so it should be implements Comparable<Color>
  • Then change your compareTo method to
public int compareTo(Color o) {
    return this.GetIsPrimary().compareTo(o.GetIsPrimary());
}
  • Change your getters and setters and primary field to use Boolean rather than boolean. Then you will be able to use compareTo as it is now an object.

Note:

  • methods, by convention, should start with lower case letters.
  • if a method returns a boolean, it is usually preceded with is. Like isPrimary().

Also note that you don't need to implement the Comparable interface to sort. Assuming the colorList is mutable, you can do it as follows by using a Comparator in the sort method.

colorList.sort(Comparator.comparing(Color::GetIsPrimary));
WJS
  • 36,363
  • 4
  • 24
  • 39