String[] meg ={"1#zhang#3207237”,"2#Wang#3207232“。。。}
根据最后的数字大小排序后输出
public class test {
public static void main(String[] arg0){
String[] Info = {"1#zhang#3207237","2#Wang#3207232"};
Map<String, Integer> myMap = new LinkedHashMap();
fillMap(myMap, Info);
myMap = sortMap(myMap);
printMap(myMap);
}
public static void fillMap(Map<String, Integer> myMap, String[] Info){
int i, j;
for(i = 0; i < Info.length; i++){
for(j = Info[i].length() - 1; j >= 0; j--){
if(Info[i].charAt(j) == '#') break;
}
int numLen = Info[i].length() - j;
String strNum = Info[i].substring(
Info[i].length() - numLen + 1, Info[i].length());
Integer num = Integer.valueOf(strNum);
// System.out.println(num);
myMap.put(Info[i], num);
}
}
private static void printMap(Map map){
Iterator it = map.entrySet().iterator();
while(it.hasNext()){
Map.Entry entry = (Map.Entry) it.next();
System.out.println(entry.getKey());
}
}
public static Map sortMap(Map oldMap) {
ArrayList<Map.Entry<String, Integer>> list =
new ArrayList<Map.Entry<String, Integer>>(oldMap.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Entry<java.lang.String, Integer> arg0,
Entry<java.lang.String, Integer> arg1) {
return arg0.getValue() - arg1.getValue();
}
});
Map newMap = new LinkedHashMap();
for (int i = 0; i < list.size(); i++) {
newMap.put(list.get(i).getKey(), list.get(i).getValue());
}
return newMap;
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。