設定されたフレーム - コンパレータ(比較器インタフェース)

まず、基本的な意味

タイプ:インタフェース。
意味:匹敵します。
一時的な比較ルール:ルールの比較。などのツール。
メソッドを実装する必要があります。int()を比較します。負の(小さい)ゼロ(等しい)正(大)。

第二に、例 - コンパレータインタフェース実装

public class Course implemants Comparator{
  private String id;
  private String name;
  public Course(){
  }
  public Course(String id,String name){
    this.id = id;
    this.name = name;
  }
  @Override
  public int compare(Course1 o1, Course2 o2){
    return o1.name.compare(o2.name); // 此时为升序排列。
    //return o2.name.compare(o1.name); // 此时为降序排列。
  }
}

** Collection.sort()**メソッドを呼び出します。

List<Course> courseList = new ArrayList<Course>();
Course[] courseArray = {new Course("1","语文"),new Course("3","英语"),new Course("2","数学")};
courseArray.addAll(Arrays.asList(courseList));
Collections.sort(courseArray, new Course());

おすすめ

転載: blog.csdn.net/lizengbao/article/details/86747553