public class PersonalService {
/**
* 对map根据key值排序
* @param map
* @return
*/
public static String[] sortMapKey(Map<String, Object> map) {
// 提取map的key
String[] keyArr = map.keySet().toArray(new String[map.size()]);
// 根据自定义的比较器进行排序
Arrays.sort(keyArr, new PersonalService().new ComparMap(map));
return keyArr;
}
/**
* 定义一个内部类(为了能够访问map数据),用作比较器
*/
class ComparMap implements Comparator<String> {
private Map<String,Object> map;
public ComparMap(Map<String, Object> map) {
super();
this.map = map;
}
// 覆写比价方法
@Override
public int compare(String o1, String o2) {
if(Integer.parseInt(o2) > Integer.parseInt(o1)) {
return 1;
}else if(Integer.parseInt(o2) < Integer.parseInt(o1)) {
return -1;
}else {
return o2.compareTo(o1);
}
}
}
}