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. 搭建C#框架 博文观感

    最近刚开始着手做项目,在后期开发的时候遇到不少预期之外的问题,而且工期也超出预算不少.反思了一下,主要是做的项目少,前期需求分析不明朗,当然对于框架也没有意识.凡此种种.当然,遇到问题就要去想办法解决 ...

  2. linux 图形界面切换

    开机为文本界面,由文本界面切换到图形界面:    方法1:运行命令          #startx , 需要先配置图形界面信息,(暂时不会~) :    方法2:修改/etc/inittab文件中的 ...

  3. Spring-----Spring Jar包

    转载自:http://blog.csdn.net/hekewangzi/article/details/51713110

  4. Spring中ref local与ref bean区别

    今天在做SSH框架Demo实例时,在ApplicationResources.properties文件时对<ref bean>与<ref local>感到不解,经查找资料才弄明 ...

  5. 监听enter事件

    document.onkeydown=keyDownSearch; function keyDownSearch(e) { // 兼容FF和IE和Opera var theEvent = e || w ...

  6. 提示constructor无法location的原因

    1.缺少对应属性的set方法 2.缺少确实没有对应的方法 3.对应的构造方法中参数类型不匹配 4.java对象不会在寻找构造函数时执行数据类型的强制类型转换,没有对应的类型就返回异常,不会自动强制转换 ...

  7. [LeetCode]题解(python):155-Min Stack

    题目来源: https://leetcode.com/problems/min-stack/ 题意分析: 实现一个小的栈,包括初始化,push,pop,top,和getMin. 题目思路: 私用是用两 ...

  8. lua学习笔记1

    lua中调用c的函数 #include <stdio.h> #include <string.h> #ifdef __cplusplus extern "C" ...

  9. QCA4002/QCA4004 为主流家电和消费电子产品推出低功耗Wi-Fi平台

    美国高通公司日前宣布,其子公司高通创锐讯推出全新芯片系列,这是低功耗Wi-Fi解决方案系列的一部分,可连接组成物联网的各种设备.QCA4002和QCA4004网络平台在芯片上纳入IP堆栈及完整的网络服 ...

  10. Make Hadoop 1.2.1 run, my first try

    经历两天努力,8月25日下午2点40分,终于让hadoop1.2.1跑起来. 用的是<Hadoop实战第2版>(陆嘉恒)里面的WordCount例子,虽然书是2013年出的,但用的例子还是 ...