オブジェクトをコレクションに並べ替え、オブジェクトのインジケーターのサイズに応じて昇順または下降順序をソートします。コードは次のとおりです。
注文の注文
下降注文のソートコレクションを実施する(リスト、new Comparator <sultstypedesc>(){public int compare(resulttypedesc o1、resulttypedesc o2){return o2.getratio()。比較(o1.getratio();}}});昇順の注文を手配します
collections.sort(list、new Comparator <sultstypedesc>(){public int compare(resulttypedesc o1、resulttypedesc o2){return o1.getratio()。compareto(o2.getratio());}});テスト後、2つのオブジェクトの位置を昇順または降順に変更するだけであることがわかりました。
メトリックが同じ場合は、複数のメトリックに従って並べ替え、比較器を作成する必要があります。
import java.util。*; public class compariatorresulttype実装Comparator {public compare(object arg0、object arg1){resulttypedesc desc0 =(resulttypedesc)arg0; resulttypedesc desc1 =(resulttypedesc)arg1; //最初にメインインジケーターを比較します。メインインジケーターが同じ場合、セカンダリインジケーターint flag = desc0.getxxx()。比較(desc1.getxxx())を比較してください。 if(flag == 0){return desc0.getxxx2()。比較(desc1.getxxx2()); } else {return flag; }}} //テストクラスのコード:comparitorresulttype comparator = new compartorresulttype(); collections.sort(list、comparator);リストセットの逆出力:
collections.reverse(list);
resultTypedESCは必要なエンティティクラスオブジェクトであり、独自のコードと組み合わせて使用できます。
この方法では、ヌルポインターを報告し、状況を組み合わせて自分で解決し、nullかどうかを判断する場合があります。