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?