2016-08-08 3 views
0

Я пытаюсь реализовать пользовательский компаратор для рамки Beacon (AltBeacon).В дополнение к пользовательскому компаратору

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

Это терпит неудачу с ошибкой при слежении:

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

+0

Это может помочь - добавьте тип в свой запрос Arrays.asList (маяки): http://stackoverflow.com/questions/15799359/is-there-a-way-to-force-the-return-type-of -arrays-aslist –

+0

Похоже, что «beacons» уже является объектом «Collection», поэтому зачем его помещать в «Список»? –

ответ

1

Collections.sort() сортирует список INPLACE.

Что вы делаете, это передать ему новый список, но после его вызова он больше не доступен для вас, поэтому он бесполезен.

Сначала поместите маяки в список, затем выполните сортировку, затем используйте отсортированный список.

Смежные вопросы