String[] meg ={"1#zhang#3207237”,"2#Wang#3207232“。。。}根據最後的數字大小排序後輸出
幫助lz實現了一下,望采納
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
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;
}
}