开发者社区> 问答> 正文

Java中如何截取字符串数组中一段数字按大小进行排序,再输出整个排序后的字符串?

String[] meg ={"1#zhang#3207237”,"2#Wang#3207232“。。。}根据最后的数字大小排序后输出

展开
收起
蛮大人123 2016-03-20 10:56:51 5591 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪
    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;  
        }  
    }
    2019-07-17 19:09:06
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载