对 List 里面 Map 的指定 key 进行排序

 

对 List 里面 Map 的指定 key 进行排序,重写Collections.sort() 方法

 

package com.bugyun.tmp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试排序
 * 对 List 里面 Map 的指定 key 进行排序
 * @author bugyun
 *
 */
public class Tmp {
	
	public static void main(String[] args) {
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		Map<String,Object> mapOne = new HashMap<String, Object>();
		Map<String,Object> mapTwo = new HashMap<String, Object>();
		Map<String,Object> mapThree = new HashMap<String, Object>();
		Map<String,Object> mapFour = new HashMap<String, Object>();
		Map<String,Object> mapFive = new HashMap<String, Object>();
		Map<String,Object> mapSix = new HashMap<String, Object>();
		
		mapOne.put("gradeId", "1");
		mapOne.put("clzssId", "1");
		mapOne.put("score", "20");
		
		mapTwo.put("gradeId", "1");
		mapTwo.put("clzssId", "2");
		mapTwo.put("score", "25");
		
		mapThree.put("gradeId", "1");
		mapThree.put("clzssId", "3");
		mapThree.put("score", "18");
		
		
		mapFour.put("gradeId", "1");
		mapFour.put("clzssId", "3");
		mapFour.put("score", "25");
		
		mapFive.put("gradeId", "1");
		mapFive.put("clzssId", "2");
		mapFive.put("score", "18");
		
		mapSix.put("gradeId", "1");
		mapSix.put("clzssId", "4");
		mapSix.put("score", "25");
		
		list.add(mapOne);
		list.add(mapTwo);
		list.add(mapThree);
		list.add(mapFour);
		list.add(mapFive);
		list.add(mapSix);
		
		sort(list,"score");
		getGradeRank(list);

	}
	
	
	/**
	 * 对 List 的 Map 里 key=args 的 double 行进行由大到小的排序
	 * @param list
	 * @param args
	 */
	private static void sort(List<Map<String, Object>> list, final String args) {
		Collections.sort(list, new Comparator<Map<String,Object>>() {
			public int compare(Map<String,Object> gradeScoreOne, Map<String,Object> gradeScoreTwo) {
				int flag = 0 ;
					Double  first = Double.valueOf(gradeScoreOne.get(args).toString());
					Double  second = Double.valueOf(gradeScoreTwo.get(args).toString());
					
					if((first - second) > 0){
						flag = -1 ;
					}else if((first - second) < 0){
						flag = 1 ;
					}else{
						flag = 0 ;
					}
					return flag ;
			};
		});
	}
	
	
	/**
	 * 根据 List 里的 Map 的 score 排序,确定年级名次
	 * @param list
	 */
	private static void getGradeRank(List<Map<String, Object>> list){
		int gradeRank = 0;
		int tempRank = 0;
		Long tempGradeId = 0L;
		Long gradeId = 0L ;
		Double tempScore = Double.MAX_VALUE;
		
		for (Map<String, Object> gradeScore : list){
//			取得年级的ID
			gradeId = Long.valueOf(gradeScore.get("gradeId").toString());
//			取得年级考试分数
			Double examScore = Double.valueOf(gradeScore.get("score").toString());
			
//			年级第一次读取的时候,走该条语句
			if (!((gradeId).equals(tempGradeId))) {
				gradeRank = 1;
				tempRank = 0;
				tempScore = examScore;		//考试分数
				tempGradeId = gradeId;		//班级id
			} else if ((examScore).equals(tempScore) ) {
//				tempRank++;
			} else if (Double.doubleToLongBits(examScore)<Double.doubleToLongBits(tempScore)) {
				tempScore = examScore;
				gradeRank = ++gradeRank + tempRank;
				tempRank = 0;
			}
			gradeScore.put("gradeRank", gradeRank);
		}

		for(Map<String,Object> map : list){
			System.out.println(map.entrySet());
		}
	}

}

 

你可能感兴趣的:(java)