我需要制作一个比较器,通过其中一个long
类型的变量对对象列表进行排序。
public class ParticipantIndexComparator implements Comparator<Integer> {
final List<Participant> participants;
public ParticipantIndexComparator(ArrayList<Integer> numbersToSort) {
participants=new ArrayList<Participant>();
for (int i=0;i<numbersToSort.size();i++)
{ participants.add(i,competition.participant.get(numbersToSort.get(i))); participants.get(i).comparator=numbersToSort.get(i);}
}
@Override
public int compare(Integer i1, Integer i2) {
long l1 = participants.get(i1).kpTime.get(kpSelected);
long l2 = participants.get(i2).kpTime.get(kpSelected);
return Long.compare(l1, l2);
}
}
但是return Long.compare(l1, l2);
是无效的 - "The method compare(long, long) is undefined for the type Long".
看起来我做错了。
participants.get(i).comparator=numbersToSort.get(i)
为什么要将单个数字赋值给名为comparator
的东西? - kajacx