Java - 比较器

时间:2013-11-23 12:08:03

标签: java android arraylist comparator

我在比较器上遇到了问题 - 我需要对我的ArrayList<Integer> numbersToSort进行排序,它将数字链接保存到competition.participant.get(numbers.get(index)) - 所以我有一个主要的对象 - 竞争对象列表。因此,我的ArrayList<Integer> numbersToSor t会保留我应该使用的competition.participants个数。我想出如何对我的ArrayList<Integer> numbersToSort进行排序的唯一方法是(我后来将提供给我的ListView适配器)是另一个&#34; parallel&#34; List<Participants>我将从主对象复制参与者,竞争并在ArrayList<Integer> numbersToSort中存储他们的号码,以便稍后我可以从已排序的ArrayList<Integer> sortedNumbers汇总新的List<Participant> participants。你还在吗?:)

    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); 
//time from selected checkpoint
            long l2 = participants.get(i2).kpTime.get(kpSelected);
            return (long) compare(l1, l2);
        }
    }

嗯,问题是最后一个return (long) compare(l1, l2); - 是的,我知道它不对,但这是第30次尝试:)请纠正错误

2 个答案:

答案 0 :(得分:2)

@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);
}

答案 1 :(得分:0)

试试这个:

        public int compare(Integer lhs,
                        Integer rhs) {
                    return lhs - rhs;
                }
            });
相关问题