12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- package cn.com.qmth.examcloud.commons.util;
- import java.util.Comparator;
- import java.util.Map;
- import java.util.TreeMap;
- /**
- * 集合工具
- *
- * @author WANGWEI
- * @date 2019年5月15日
- * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
- */
- public class MapUtil {
- /**
- * map按key排序
- *
- * @author WANGWEI
- * @param map
- * @param asc
- * 升序
- * @return
- */
- public static <T> Map<String, T> sortMapByKey(Map<String, T> map, boolean asc) {
- if (map == null) {
- return null;
- }
- Map<String, T> sortMap = new TreeMap<String, T>(new Comparator<String>() {
- @Override
- public int compare(String o1, String o2) {
- if (asc) {
- return ((String) o1).compareTo((String) o2);
- } else {
- return ((String) o2).compareTo((String) o1);
- }
- }
- });
- sortMap.putAll(map);
- return sortMap;
- }
- }
|