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接口是一种补救的做法。

利用Comparator排序的更多相关文章

  1. POJ - 3249 Test for Job (在DAG图利用拓扑排序中求最长路)

    (点击此处查看原题) 题意 给出一个有n个结点,m条边的DAG图,每个点都有权值,每条路径(注意不是边)的权值为其经过的结点的权值之和,每条路径总是从入度为0的点开始,直至出度为0的点,问所有路径中权 ...

  2. Java之——利用Comparator接口对多个排序条件进行处理

    转载自:http://blog.csdn.net/l1028386804/article/details/56513205 膜拜大神··· 一.需求 假设现在有个如此的需求:需要对一个这样的雇员列表进 ...

  3. 我的Java开发学习之旅------>Java利用Comparator接口对多个排序条件进行处理

    一需求 二实现Comparator接口 三验证排序结果 验证第一条件首先按级别排序级别最高的排在前面 验证第二条如果级别相等那么按工资排序工资高的排在前面 验证第三条如果工资相当则按入职年数排序入职时 ...

  4. 【LeetCode】Merge Intervals 题解 利用Comparator进行排序

    题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int start; * int e ...

  5. Java利用反射排序

    前言 Java为我们提供了几种排序得方法,比如Arrays和Collections类,但是前提是数组或者集合中的元素都必须实现Comparable接口,基本的数据类型都已经实现了Comparable接 ...

  6. [Java] 使用Comparator排序对象

    package test.collections; import java.util.ArrayList; import java.util.Collection; import java.util. ...

  7. Java8:使用Lambda表达式增强版Comparator排序

    学习路上的自我记录-------路好长,就问你慌不慌,大声港,不慌.----jstarseven. 实体类: package com.server.model; /** * Created by js ...

  8. Comparator 排序 ArrayList 实操练习

    package ltb6w; import java.util.Scanner;import java.util.ArrayList;import java.util.Comparator;impor ...

  9. NYOJ 8 一种排序(comparator排序)

    一种排序 时间限制: 3000 ms  |  内存限制: 65535 KB 难度: 3   描述 现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复:还知道这个长方形的宽和长,编号.长.宽都 ...

随机推荐

  1. node.js(三)url处理

    1.parse函数的基础用法 parse函数的作用是解析url,返回一个json格式的数组,请看如下示例: var url = require('url'); url.parse('http://ww ...

  2. OC中文件的操作

    OC中文件操作,在之前的文章中,已经接触到了文件的创建了,但是那不是很具体和详细,这篇文章我们就来仔细看一下OC中是如何操作文件的: 第一.首先来看一下本身NSString类给我们提供了哪些可以操作文 ...

  3. Thinkphp利用微信多客服消息推送取货二维码消息

    首先看微信官方的说法: 当用户主动发消息给公众号的时候(包括发送信息.点击自定义菜单.订阅事件.扫描二维码事件.支付成功事件.用户维权), 微信将会把消息数据推送给开发者,开发者在一段时间内(目前修改 ...

  4. hdu4745

    区间DP,这类题目还是非常常见的,可惜平时都不怎么在意.一到比赛就弱得像鸟一样,真心囧. 题目要求很简单,就是一个最长的回文子序列,输出该子序列的长度. 区间DP,最常用的一种策略(类似于数学归纳法) ...

  5. 并发编程: c++11 thread(Func, Args...)利用类成员函数创建线程

    c++11是VS2012后支持的新标准,为并发编程提供了方便的std::thread. 使用示例: #include <thread> void thread_func(int arg1, ...

  6. mysql服务端安装的系列问题处理

    1.mysql服务无法启动,报1067错误 注意:mysql服务端加压之后的文件名最好不要带其他符号(如:"_"之类等其他的一些符号),用纯英文跟数字就可以,没有经过仔细验证带符号 ...

  7. 转 git操作小结

    UNDER MIT LICENSE. 公司几乎所有的项目都是使用 git 仓库来管理代码,以前对 git 只有些肤浅的了解,每次提交代码或者上线的时候总是会提心吊胆,生怕出现一些未知的问题.经过三个月 ...

  8. js监听input等表单输入框的变化事件oninput

    js监听input等表单输入框的变化事件oninput,手机页面开发中使用到文本框textarea输入字符监听文本框变化计算还可以输入多少字符,如果使用onkeyup的话是无法监听到输入法输入的文本变 ...

  9. PHP中date函数参数详解

    date函数输出当前的时间echo date('Y-m-d H:i:s', time()); // 格式:xxxx-xx-xx xx:xx:xx 第一个参数的格式分别表示: a - "am& ...

  10. asp.net core + angular2

    asp.net core + angular2 的环境配置 国内整个对 asp.net core  和 angular2这些新出来的关注度不是太好.跟国外比很大差距. 我在试着去做这个整合的时候也碰到 ...