转载自:http://blog.csdn.net/l1028386804/article/details/56513205

膜拜大神···

一、需求

假设现在有个如此的需求:需要对一个这样的雇员列表进行排序,排序规则如下:
    1、首先级别最高的排在前面,
    2、如果级别相等,那么按工资排序,工资高的排在前面,
    3、如果工资相当则按入职年数排序,入职时间最长的排在前面。

雇员对象包含级别、工资和入职年份,代码如下:

[java] view
plain
 copy

 
  1. package

    import

    /**

  2. * 雇员信息
  3. * @author liuyazhuang
  4. *
  5. */
    publicclassimplements

    privatestaticfinallong

    * ID

  6. */
    publicint

    * 级别

  7. */
    publicint

    * 工资

  8. */
    publicint

    * 入职年数

  9. */
    publicint

    publicint
    return

    publicvoidint
    this

    publicint
    return

    publicvoidint
    this

    publicint
    return

    publicvoidint
    this

    publicint
    return

    publicvoidint
    this

    publicintintintint
    this
    this
    this
    this

    }

二、实现Comparator接口

这里我们实现Java.util.Comparator接口,用于对雇员列表进行排序,代码如下:

[java] view
plain
 copy

 
  1. package

    import

    import

    /**

  2. * 核心排序类
  3. * @author liuyazhuang
  4. *
  5. */
    publicclassimplements
  6. publicint
    int;
  7. int
    if) {
  8. ) ?  : -;
  9. else
  10. if) {
  11. ) ?  : -;
  12. else
  13. if) {
  14. ) ?  : -;
  15. return

    }

三、验证排序结果

下面用一个单元测试,来验证排序结果是否正确

[java] view
plain
 copy

 
  1. package

    import
    import
    import

    import

    import
    import

    /**

  2. * 测试排序类
  3. *
  4. * @author liuyazhuang
  5. *
  6. */
    publicclass
  7. publicvoidthrows
    new

    new, , , ));

  8. new, , , ));
  9. new, , , ));
  10. new, , , ));
  11. new, , , ));
  12. new, , , ));
  13. new, , , ));
  14. new, , , ));
  15. new, , , ));
  16. new, , , ));
  17. new, , , ));
  18. new
    );
  19. );
  20. for
    , employee.getId(), employee.getLevel(), employee.getSalary(),
  21. );
  22. }

运行结果:

四、附录

java.util.Comparator接口源代码

[java] view
plain
 copy

 
  1. /*
  2. *  Licensed to the Apache Software Foundation (ASF) under one or more
  3. *  contributor license agreements.  See the NOTICE file distributed with
  4. *  this work for additional information regarding copyright ownership.
  5. *  The ASF licenses this file to You under the Apache License, Version 2.0
  6. *  (the "License"); you may not use this file except in compliance with
  7. *  the License.  You may obtain a copy of the License at
  8. *
  9. *     http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. *  Unless required by applicable law or agreed to in writing, software
  12. *  distributed under the License is distributed on an "AS IS" BASIS,
  13. *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. *  See the License for the specific language governing permissions and
  15. *  limitations under the License.
  16. */

    package

    /**

  17. * A {@code Comparator} is used to compare two objects to determine their ordering with
  18. * respect to each other. On a given {@code Collection}, a {@code Comparator} can be used to
  19. * obtain a sorted {@code Collection} which is <i>totally ordered</i>. For a {@code Comparator}
  20. * to be <i>consistent with equals</i>, its {code #compare(Object, Object)}
  21. * method has to return zero for each pair of elements (a,b) where a.equals(b)
  22. * holds true. It is recommended that a {@code Comparator} implements
  23. * {@link java.io.Serializable}.
  24. *
  25. * @since 1.2
  26. */
    publicinterface

    * Compares the two specified objects to determine their relative ordering. The ordering

  27. * implied by the return value of this method for all possible pairs of
  28. * {@code (lhs, rhs)} should form an <i>equivalence relation</i>.
  29. * This means that
  30. * <ul>
  31. * <li>{@code compare(a,a)} returns zero for all {@code a}</li>
  32. * <li>the sign of {@code compare(a,b)} must be the opposite of the sign of {@code
  33. * compare(b,a)} for all pairs of (a,b)</li>
  34. * <li>From {@code compare(a,b) > 0} and {@code compare(b,c) > 0} it must
  35. * follow {@code compare(a,c) > 0} for all possible combinations of {@code
  36. * (a,b,c)}</li>
  37. * </ul>
  38. *
  39. * @param lhs
  40. *            an {@code Object}.
  41. * @param rhs
  42. *            a second {@code Object} to compare with {@code lhs}.
  43. * @return an integer < 0 if {@code lhs} is less than {@code rhs}, 0 if they are
  44. *         equal, and > 0 if {@code lhs} is greater than {@code rhs}.
  45. * @throws ClassCastException
  46. *                if objects are not of the correct type.
  47. */
    publicint

    * Compares this {@code Comparator} with the specified {@code Object} and indicates whether they

  48. * are equal. In order to be equal, {@code object} must represent the same object
  49. * as this instance using a class-specific comparison.
  50. * <p>
  51. * A {@code Comparator} never needs to override this method, but may choose so for
  52. * performance reasons.
  53. *
  54. * @param object
  55. *            the {@code Object} to compare with this comparator.
  56. * @return boolean {@code true} if specified {@code Object} is the same as this
  57. *         {@code Object}, and {@code false} otherwise.
  58. * @see Object#hashCode
  59. * @see Object#equals
  60. */
    publicboolean
    }

Java之——利用Comparator接口对多个排序条件进行处理的更多相关文章

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

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

  2. java Comparable 和 Comparator接口区别

    Comparable 简介 Comparable 是排序接口. 若一个类实现了Comparable接口,就意味着“该类支持排序”.  即然实现Comparable接口的类支持排序,假设现在存在“实现C ...

  3. Java Comparable 和 Comparator 接口详解

    本文基于 JDK8 分析 Comparable Comparable 接口位于 java.lang 包下,Comparable 接口下有一个 compareTo 方法,称为自然比较方法.一个类只要实现 ...

  4. 【LeetCode】two num 利用comparable接口 对对象进行排序

    题目two num 题意:给定一个整数数组和一个目标值.要求在数组中找到两个数.使得它们的和相加等于目标值.而且返回两个数的下标 思路:1.假设使用暴力,时间复杂度为O(n^2) 2.能够先将全部数进 ...

  5. java利用Comparator接口对自定义数组排序

    import java.util.Arrays; import java.util.Comparator; public class MySort { public static void main( ...

  6. java基础-Comparator接口与Collections实现排序算法

    java 排序Comparable和Comparator使用 java提供了两个排序用的接口Comparable和Comparator,一般情况下使用区别如下: Comparable 接口用于类的固定 ...

  7. Java语言利用Collections.sort对Map,List排序

    1.main方法包含TreeMap排序1,TreeMap排序2,HashMap排序,List<Integer>排序,List<Bean>排序,List<Map>排序 ...

  8. Java:Comparator接口

    public interface Comparator<T> 接口里面的方法 int compare(T o1, T o2) o1 > o2 返回 1 o1 = o2 返回 0 o1 ...

  9. java中Comparatable接口和Comparator接口的区别

    1.不同类型的排序规则 .自然排序是什么?   自然排序是一种升序排序.对于不同的数据类型,升序规则不一样:   BigDecimal BigInteger Byte Double Float Int ...

随机推荐

  1. flask自动代码自动补全

    编写py文件时,无法补全: 在app对象后面添加:# type:Flask app=Flask(__name__)   # type:Flask from flask import Flask, fl ...

  2. P2590 [ZJOI2008]树的统计(树链剖分)

    P2590 [ZJOI2008]树的统计 虽然是入门树剖模板 但是我终于1A了(大哭) 懒得写啥了(逃 #include<iostream> #include<cstdio> ...

  3. CentOS 7下搭建配置 SVN 服务器

    原文链接:https://www.cnblogs.com/tdalcn/p/6937714.html 同步:http://blog.csdn.net/u011884440/article/detail ...

  4. 原生js封装的获取某一天是当年的第几周方法

    function getWeek(str){ //str格式为yyy-mm-dd //周日归到了本周 var d=new Date(str); var day=d.getDay(); var orig ...

  5. 89. a^b【快速幂模板】

    a^b Description 求 aa 的 bb 次方对 pp 取模的值. 输入格式 三个整数 a,b,pa,b,p ,在同一行用空格隔开. 输出格式 输出一个整数,表示a^b mod p的值. 数 ...

  6. cf213E 线段树维护hash

    链接 https://codeforces.com/contest/213/problem/E 题目大意 给出两个排列a.b,长度分别为n.m,你需要计算有多少个x,使 得\(a_1 + x; a_2 ...

  7. DataTabel 与DataView之间的转化

    DataTable转为DataView,或者反之转化, 使用的是文档/试图模型,DataTable可以有多个视图,这样就可以不需要借助List类型对dataTable数据进行筛选或者排序 //Data ...

  8. 题解——洛谷P1550 [USACO08OCT]打井Watering Hole(最小生成树,建图)

    题面 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pas ...

  9. 【Mybatis】-- Mapper动态代理开发注意事项

    1.1. Mapper动态代理方式 1.1.1. 开发规范 Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对 ...

  10. Bytom矿池接入协议指南

    矿机配置 https://gist.github.com/HAOYUatHZ/a47400bde4a138825faef415387b532c 固件升级 https://service.bitmain ...