【发布时间】:2018-09-28 23:46:23
【问题描述】:
我的班级是:
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;
}
我需要做一个Comparator 类,它将首先按组对元素进行排序,然后如果它在同一组中则按数字排序。我的代码是:
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;
}
}
我不确定如何制定它以使其发挥作用。
【问题讨论】:
标签: java comparator