Map 排序写法

话不多说,直接贴代码:
//对任务单的分段计划根据里程进行排序设置顺序
public void deliverySeq(Shipment shipment) {
		Set<Leg> legs = shipment.getLegs();
		Map<String, List<Leg>> legMaps = new HashMap<String, List<Leg>>();
		String consignorCode = GlobalParamUtils
				.getGloableStringValue("CONSIGNOR_CODE");
		for (Leg leg : legs) {
			List<Leg> legList = legMaps.get(leg.getLocationHashCode());
			if (legList == null) {
				legList = new ArrayList<Leg>();
				legMaps.put(leg.getLocationHashCode(), legList);
			}
			legList.add(leg);
		}
		// 对相同的出发地,目的地的LEG进行里程归纳
		Map<String, Integer> seqMap = new HashMap<String, Integer>();
		for (Map.Entry<String, List<Leg>> me : legMaps.entrySet()) {
			Leg leg = me.getValue().get(0);
			Integer distance = this.getLegDistance(leg, consignorCode);
			seqMap.put(me.getKey(), distance);
		}
//               调用排序
		Map.Entry<String, Integer>[] seqMaps = this
				.getSortedHashtableByValue(seqMap);
		for (int i = 0; i < seqMaps.length; i++) {
			List<Leg> legList = legMaps.get(seqMaps[i].getKey());
			for (Leg leg : legList) {
				leg.setDeliverySeq(i + 1);
				commonDao.store(leg);
			}
		}
	}

//Map的Integer值排序的写法
public Map.Entry<String, Integer>[] getSortedHashtableByValue(
			Map<String, Integer> map) {
		Set set = map.entrySet();
		Map.Entry[] entries = (Map.Entry[]) set.toArray(new Map.Entry[set
				.size()]);
		Arrays.sort(entries, new Comparator<Object>() {
			public int compare(Object arg0, Object arg1) {
				Long key1 = Long.valueOf(((Map.Entry<String, Integer>) arg0)
						.getValue().toString());
				Long key2 = Long.valueOf(((Map.Entry<String, Integer>) arg1)
						.getValue().toString());
				return key1.compareTo(key2);
			}
		});
		return entries;
	}

你可能感兴趣的:(map)