加载中…
个人资料
null
null
  • 博客等级:
  • 博客积分:0
  • 博客访问:3,914
  • 关注人气:27
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Java中Comparator多级排序的实现

(2015-11-18 00:01:32)
标签:

comparator

多级排序

java

分类: J2SE

JDK里的Comparator方法为用户提供了自定义排序规则的接口,而我们在实际使用是一般也只是用了一个排序维度,本文介绍下多级或多个维度的排序需求下的接口实现。

       我们假设一个雇员对象,包含级别、工资和入职年份(为了简单,这些属性写成 public),代码如下:

 

Java代码 
  1. public Class Employee  
  2.    
  3.     public int level;  //级别  
  4.     public int salary; //工资  
  5.     public int years;  //入职年数  
  6.    
  7.  

       现在我需要做一个这样的雇员列表,首先级别最高的排在前面,如果级别相等,那么按工资排序,工资高的排在前面,如果工资相当则按入职年数排序,入职时间最长的排在前面。

       因为雇员列表是在内存中的一个 List 对象,现在要对这个 List 中的雇员按上面的要求进行排序 Collections.sort(employeeList, comparator),那么这个 Comparator 应该怎么写呢?

下面这种肯定是不对的

Java代码 
  1. Comparator cp_by_default new Comparator(){  
  2.     @Override  
  3.     public int compare(Employee a1, Employee a2)  
  4.         int a2.level a1.level;  
  5.         if(a 0 
  6.             return a;  
  7.         a2.salary a1.salary;  
  8.         if(a 0 
  9.             return a;  
  10.         return a2.years a1.years;  
  11.             
  12. };  

 

 正确参考代码写法如下

Java代码 
  1. package net.oschina.tester;  
  2.    
  3. import java.util.ArrayList;  
  4. import java.util.Collections;  
  5. import java.util.Comparator;  
  6. import java.util.List;  
  7.    
  8.   
  9. public class Employee  
  10.    
  11.     public static void main(String[] args)  
  12.         List objs new ArrayList(){{  
  13.             add(new Employee(5,3,5000,2));  
  14.             add(new Employee(1,9,10000,10));  
  15.             add(new Employee(4,5,8000,6));  
  16.             add(new Employee(2,9,12000,7));  
  17.             add(new Employee(6,1,2000,1));  
  18.             add(new Employee(3,5,8000,12));  
  19.         }};  
  20.         Collections.sort(objs, comparator);  
  21.         System.out.println("No\tLevel\tSalary\tYears\n=============================");  
  22.         for(Employee objs)  
  23.             System.out.printf("%d\t%d\t%d\t%d\n",a.id,a.level,a.salary,a.year);  
  24.      
  25.    
  26.     public Employee(int id, int level, int salary, int year){  
  27.         this.id id;  
  28.         this.level level;  
  29.         this.salary salary;  
  30.         this.year year;  
  31.      
  32.        
  33.     public int id;  
  34.     public int level;  
  35.     public int salary;  
  36.     public int year;  
  37.    
  38.     private final static Comparator comparator new Comparator(){  
  39.         @Override  
  40.         public int compare(Employee a1, Employee a2)  
  41.             int cr 0 
  42.             int a2.level a1.level;  
  43.             if(a != 0 
  44.                 cr (a>0)?3:-1 
  45.             else 
  46.                 a2.salary a1.salary;  
  47.                 if(a != 0 
  48.                     cr (a>0)?2:-2 
  49.                 else 
  50.                     (int)(a2.year a1.year);  
  51.                     if(a != 0 
  52.                         cr (a>0)?1:-3 
  53.                  
  54.              
  55.             return cr;  
  56.                 
  57.     };  
  58.        
  59.  

 上面参考方法给出了3级排序规则下的接口实现方法,2级或3级以上的接口实现方式类似。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有