0

I am trying to implement a custom comparator for Beacon class (AltBeacon) framework.

      Collections.sort(Arrays.asList(beacons), new Comparator<Beacon>(){
       @Override
       public int compare(Beacon lhs, Beacon rhs) {
           double diff =  lhs.getDistance() - rhs.getDistance();

           if (diff > 0 ){
               return 1;
           }

           if (diff < 0 ){
               return -1;
           }


           return 0;
       }
   });

It fails with a following error:

Error:(176, 19) error: no suitable method found for sort(List<Collection<Beacon>>,<anonymous Comparator<Beacon>>) method Collections.<T#1>sort(List<T#1>) is not applicable (cannot infer type-variable(s) T#1 (actual and formal argument lists differ in length)) method Collections.<T#2>sort(List<T#2>,Comparator<? super T#2>) is not applicable (inferred type does not conform to upper bound(s) inferred: Collection<Beacon> upper bound(s): Beacon,Object) where T#1,T#2 are type-variables: T#1 extends Comparable<? super T#1> declared in method <T#1>sort(List<T#1>) T#2 extends Object declared in method <T#2>sort(List<T#2>,Comparator<? super T#2>)

2
  • This may help - add type to your Arrays.asList(beacons) call : stackoverflow.com/questions/15799359/… Commented Aug 8, 2016 at 19:18
  • It looks like beacons is already a Collection object, so why put it into a List? Commented Aug 8, 2016 at 19:21

1 Answer 1

1

Collections.sort() sorts the list inplace.

What you are doing is passing it a new List, but after the call it's not available to you anymore, so it's useless.

Put the beacons in a List first, then sort it, then use the sorted List.

Sign up to request clarification or add additional context in comments.

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.