import java.util.Comparator;

class Studentxx {
    private String nameString;
    private int age;

    public Studentxx(String nameString, int age) {
        // super();
        this.nameString = nameString;
        this.age = age;
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof Studentxx)) {
            return false;
        }
        Studentxx studentxx = (Studentxx) obj;
        if (studentxx.nameString.equals(this.nameString)
                && studentxx.age == this.age) {
            return true;
        } else {
            return false;
        }
    }

    public String getNameString() {
        return nameString;
    }

    public void setNameString(String nameString) {
        this.nameString = nameString;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Studentxx [nameString=" + nameString + ", age=" + age + "]";
    }

}

class StudentComparator implements Comparator<Studentxx> {
    public int compare(Studentxx s1, Studentxx s2) {
        if (s1.equals(s2)) {
            return 0;
        } else if (s1.getAge() < s2.getAge()) {
            return 1;
        } else {
            return -1;
        }
    }
}

public class ComparatorDemo {
    public static void main(String[] args) {
        Studentxx stu[] = { new Studentxx("von", 20), new Studentxx("lee", 23),
                new Studentxx("wong", 29), new Studentxx("cong", 23),
                new Studentxx("sun", 39), new Studentxx("chao", 24) };
        java.util.Arrays.sort(stu, new StudentComparator());
        for (int i = 0; i < stu.length; i++) {
            System.out.println(stu[i]);
        }
    }
}

java.util.Arrays.sort(Studentxx[] a, Comparator<? super Studentxx> c):

Sorts the specified array of objects according to the order induced by the specified comparator. All elements in the array must be mutually comparable by the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the array).

This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.

The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n*log(n) performance.

Parameters:
a the array to be sorted
c the comparator to determine the order of the array. A null value indicates that the elements' natural ordering should be used.
Throws:
ClassCastException - if the array contains elements that are not mutually comparable using the specified comparator.

comparator和comparable两个接口都可以实现相投的排序功能,但是与comparable接口相比,comparator接口是一种补救的做法。

最新文章

  1. dll导入导出宏定义,出现“不允许 dllimport 函数 的定义”的问题分析
  2. 通过style控制圆形imageView显示
  3. 深入理解Linux修改hostname
  4. Retrofit学习笔记01
  5. 不得不玩玩NHibernate
  6. 关于 tomcat nio connector, servlet 3.0 async, spring mvc async 的关系
  7. mysql 插入中文时出现ERROR 1366 (HY000): Incorrect string value: &#39;\xC0\xEE\xCB\xC4&#39; for column &#39;usern ame&#39; at row 1
  8. Install Apache, PHP And MySQL On CentOS 7 (LAMP)
  9. 分享一个导出Excel时页面不跳转的小技巧
  10. web app页面要求
  11. POJ1038 - Bugs Integrated, Inc.(状态压缩DP)
  12. foreach的一点理解
  13. jquery——zTree, 完美好用的树插件
  14. HDU 4415 - Assassin’s Creed
  15. a 标签的四个伪类
  16. 数据分析≠Hadoop+NoSQL
  17. 【ASP.NET Web API教程】2.3 与实体框架一起使用Web API
  18. JS模拟窗口
  19. CSS3动画--过渡效果
  20. python全栈开发-Day13 内置函数

热门文章

  1. IsNullOrEmpty和s == null || s.Length == 0哪个快
  2. C++ ASCII 码的一些问题
  3. CodeForces 379 D. New Year Letter
  4. 转:JS在文本域鼠标指定位置插入文本-柯乐义
  5. 工作备份 build.gradle
  6. php数据类型有哪些?
  7. 关于yii2的gridview关联搜索步骤
  8. Ajax与C#应用详细实例
  9. tail -f
  10. Get Intellisense for .axml files in Visual Studio