0

I got question on Comparator - I need to sort my ArrayList<Integer> numbersToSort, which keeps numbers- links to competition.participant.get(numbers.get(index)) - so I have a main Object - competition which has a List of participants object. So, my ArrayList<Integer> numbersToSort keeps which numbers of competition.participants should I use. The only way I figured how to sort my ArrayList<Integer> numbersToSort (which I later will feed to my ListView Adapter) is to make another "parallel"List<Participants> where I shall copy participants from main object, competition and store in them their number in ArrayList<Integer> numbersToSort so I can later assemble a new ArrayList<Integer> sortedNumbers from sorted List<Participant> participants. Are you still there ?:)

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

well, the problem is the last return (long) compare(l1, l2); - yes, I know it's not right but it's the 30th attempt :) please correct what's wrong

3
  • 1
    Why cast to long? Have you tried removing the cast? That looks like a recursive call. Try returning (int)(l1-l2) Commented Nov 23, 2013 at 12:12
  • 1
    Correction. Need to compare the int values passed to the compare() method. return i1 - i2. Commented Nov 23, 2013 at 12:20
  • because I need to compare exactly theese 2 longs Commented Nov 23, 2013 at 13:40

2 Answers 2

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

The method compare(long, long) is undefined for the type Long
well, I've found that this is for Java 7+ and I'm on Android sdk which is 6, so it's a little different . it's return Long.valueOf(l1).compareTo(Long.valueOf(l2)); - plz fix it and I'll accept answer.. well, your answer is right anyway:) so I'm accepting
@user2976267 Just copy method to your project: grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/… And, You can use Java7 on Android: tools.android.com/recent/androidstudio032released
0

try this:

        public int compare(Integer lhs,
                        Integer rhs) {
                    return lhs - rhs;
                }
            });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.