`
zhanghw0917
  • 浏览: 182952 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

实现Comparator的比较器

    博客分类:
  • Java
 
阅读更多
 

比较的对象类

 

class GroupInfo {
	public String groupID; // group id or room id

	private int rank; // the position of group after sort

	private double amount; // the amount of all devices in this room or group

	private String name; // room's name

	private int bitMapResoureceId;

	public GroupInfo(String gID, Amst amst) {
		groupID = gID;
		if (amst != null) {
			name = amst.getGmst_description();
		}
	}

	public double addAmount(Amst amst) {
		if (amst != null) {
			amount = amount + amst.getAmeas_value();
		}
		return amount;
	}

	public String getName() {
		return name;
	}

	public int getRank() {
		return rank;
	}

	public void setRank(int rank) {
		this.rank = rank;
	}

	public double getAmount() {
		return amount;
	}

	public int getBitMapResoureceId() {
		return bitMapResoureceId;
	}

	public void setBitMapResoureceId(int bitMapResoureceId) {
		this.bitMapResoureceId = bitMapResoureceId;
	}
}

 

排序的方法:

 public List<GroupInfo> sortGroupInfo(List<GroupInfo> gilist) {
        Collections.sort(gilist, new GIComparator(GIComparator.SORT_TYPE_AMOUNT));
        int size = gilist.size();
        if (size > MARK_DIGIT){ // if the list size greater than MARK_DIGIT 
            List<GroupInfo> gilist_h = (gilist.subList(0, MARK_DIGIT));
            List<GroupInfo> gilist_t= (gilist.subList(MARK_DIGIT, size));
            Collections.sort(gilist_t, new GIComparator(GIComparator.SORT_TYPE_GROUPID));
//            gilist.clear();  //此处不能将gilist 清空,sublist方法只是返回的视图,其实和父list使用的是同一块内存区域
            List<GroupInfo> result_list = new ArrayList<GroupInfo>();
            for (GroupInfo gi : gilist_h){
                result_list.add(gi);
            }
            for (GroupInfo gi : gilist_t){
                result_list.add(gi);
            }
            gilist = result_list ;
            
        }
        return gilist;

    }

 

 

实现Comparator接口的类

 

class GIComparator implements Comparator<GroupInfo> {
    public static final int SORT_TYPE_AMOUNT = 1 ; // sort by  GroupInfo.amount 
    public static final int SORT_TYPE_GROUPID = 2 ; // sort by  GroupInfo.groupID
 
    private int sort_type ; // the list sort type 
    public GIComparator(int type){
        sort_type = type ;
    }
    
    @Override
    public int compare(GroupInfo arg0, GroupInfo arg1) {
        double amount0 = arg0.getAmount();
        double amount1 = arg1.getAmount();

        String groupID0 = arg0.groupID;
        String groupID1 = arg1.groupID;

        int result = 0;
        
        switch (sort_type){
            default :
            case SORT_TYPE_AMOUNT : //sort by amount and group id
                double tmpd = amount0 - amount1;
                result = (tmpd != 0 ?(tmpd > 0 ? -1 : 1):groupID0.compareTo(groupID1) );      
                break ;
            case SORT_TYPE_GROUPID : // sort by group id
                result = groupID0.compareTo(groupID1);
                break ;

        }
        
        return result;
    }

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics