2016-08-08 3 views
0

ビーコンクラス(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

'ビーコン 'はすでに' Collection'オブジェクトであるようです。なぜそれを 'List'に入れるのでしょうか? –

答えて

1

Collections.sort()は、リストのインプレースをソートします。

あなたがやっていることは新しいリストを渡すことですが、通話後はもう利用できませんので無駄です。

最初にビーコンをリストに入れてから並べ替え、ソートされたリストを使用します。

関連する問題