I have my class as:
public class Element {
int number, group, period;
String symbol, name;
double weight;
public Element(int number, String symbol, String name, int group, int period, double weight) {
this.number = number;
this.symbol = symbol;
this.name = name;
this.group = group;
this.period = period;
this.weight = weight;
}
I need to do a Comparator
class that will first sort the Element by the group then if its in the same group sort by number. My code for that is:
class GroupComparator implements Comparator<Element> {
@Override
public int compare(Element a, Element b) {
return a.group < b.group ? -1 : a.group == b.group ? a.number-b.number : 1;
}
}
I'm not sure how to formulate it to make it work.