当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

[代码片段]排序相关

发表于: 2015-01-05   作者:53873039oycg   来源:转载   浏览:
摘要:         public void scoreRank() { Double[] scoreArr = new Double[9]; scoreArr[2] = scoreArr[4] = 66.5; scoreArr[0] = scoreArr[5] = 67d; scoreArr[1] = sc

       

public void scoreRank() {
		Double[] scoreArr = new Double[9];
		scoreArr[2] = scoreArr[4] = 66.5;
		scoreArr[0] = scoreArr[5] = 67d;
		scoreArr[1] = scoreArr[3] = scoreArr[6] = 66.4;
		scoreArr[7] = 68d;
		scoreArr[8] = 66.2;
		List<Double> doubleList = Arrays.asList(scoreArr);
		List<Integer> rankList = new ArrayList<Integer>();
		Collections.sort(doubleList, new Comparator<Double>() {
			public int compare(Double arg0, Double arg1) {
				return arg1.compareTo(arg0);// 降序排列
			}
		});
		for (Double double2 : doubleList) {
			System.out.print(double2 + "\t");
		}
		System.out.println();
		// 计算rank
		int rank = 0;
		// 前一个分数,初始化为一个很大的值
		double lastScore = Integer.MAX_VALUE;
		int nOrder = 1; // 并列名次的个数
		for (Double double2 : doubleList) {
			if (lastScore != double2) {
				// 当前分数和前一个分数不相等时求出名次:(上一个名次加上并列名次),否则认为是并列名次
				rank = rank + nOrder;
				lastScore = double2;
				nOrder = 1;
			} else {
				nOrder = nOrder + 1;
			}
			rankList.add(rank);
		}
		for (Integer rank2 : rankList) {
			System.out.print(rank2 + "\t");
		}
		System.out.println();
	}

   结果为:

   

68.0	67.0	67.0	66.5	66.5	66.4	66.4	66.4	66.2	
1	2	2	4	4	6	6	6	9

 

 

 

[代码片段]排序相关

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号