例如:                  String str1 = "D1" ;
String str2 = "D11" ;
String str3 = "D12" ;
String str4 = "D2" ;
String str5 = "D3" ;
String str6 = "H" ;
String str7 = "FO" ;
如何得到想要的結果: D1,D2,D3,D11,D12,F0,H
時間有點趕,有沒有現成的解決方案,謝謝大家!

解决方案 »

  1.   


    String ss[] = {"D12","D2","D03","B","D11","D8","D12","H","F0"};
    Arrays.sort(ss);
    for(String s : ss){
    System.out.print(s+",");
    }out->B,D03,D11,D12,D12,D2,D8,F0,H,
      

  2.   

        String[] array = new String[] {
            "D1", "D11", "D12", "D13", "D2", "D3", "H", "F0", "h1", "h0", "f1",
            "F2", "d9", "1"
        };
        // 假设所有情况都是字母在前,数字在后
        Comparator<String> cmp = new Comparator<String>() {      public int compare(String str1, String str2) {        // 字母部分
            String alphabet1 = str1.replaceAll("\\d+$", "");
            String alphabet2 = str2.replaceAll("\\d+$", "");        // 如果不想区分大小写,否则compareTo
            int cmpAlphabet = alphabet1.compareToIgnoreCase(alphabet2);
            if (cmpAlphabet != 0) {
              return cmpAlphabet;
            }        // 数字部分
            String numeric1 = str1.replaceAll("^[a-zA-Z]+", "");
            String numeric2 = str2.replaceAll("^[a-zA-Z]+", "");
            if ("".equals(numeric1)) {
              // 即使numeric2也是空串也无所谓,当然,如果比较的不是String(或其他immutable对象)则另当别论
              return -1;
            }
            if ("".equals(numeric2)) {
              return 1;
            }
            int num1 = Integer.parseInt(numeric1);
            int num2 = Integer.parseInt(numeric1);
            return num1 - num2;
          }
        };
        Arrays.sort(array, cmp);    System.out.println(Arrays.toString(array));
      }
      

  3.   

    不知道,我的理解有什么很严重的问题,难道,你是想,
    代码: String str1 = "..."; String str2 ... String strx = "..." 得到String str = "xxx,xxx,xxx,xxx,xxx";如果没有理解错的话,上面的代码,关键步骤已经给出。这里是授人以渔的地方。剩下的如果还不能理解,就是资质问题了。
    如果要手把手把所有代码贴出,那么请先双手奉上您的工资。
      

  4.   

    搞定了,把代码贴下:
    DetectContentOfSampleContent [] detectContentOfSampleContentArr2 = sample.getDetectContentOfSampleContentes().toArray(detectContentOfSampleContentArr1);
    Comparator<DetectContentOfSampleContent> cmp = new Comparator<DetectContentOfSampleContent>() {       public int compare(DetectContentOfSampleContent str1, DetectContentOfSampleContent str2) {      if(str1.getDetect_of_content_shortname()==null||"null".equals(str1.getDetect_of_content_shortname())){
         str1.setDetect_of_content_shortname("") ;
         }
         if(str2.getDetect_of_content_shortname()==null||"null".equals(str2.getDetect_of_content_shortname())){
         str2.setDetect_of_content_shortname("");
         }
            String alphabet1 = str1.getDetect_of_content_shortname().replaceAll("\\d+$", "");
            String alphabet2 = str2.getDetect_of_content_shortname().replaceAll("\\d+$", "");
            int cmpAlphabet = alphabet1.compareToIgnoreCase(alphabet2);
            if (cmpAlphabet != 0) {
              return cmpAlphabet;
            }         String numeric1 = str1.getDetect_of_content_shortname().replaceAll("^[a-zA-Z]+", "");
            String numeric2 = str2.getDetect_of_content_shortname().replaceAll("^[a-zA-Z]+", "");
            if ("".equals(numeric1)) {
              return 1;
            }
            if ("".equals(numeric2)) {
              return -1;
            }
            int num1 = Integer.parseInt(numeric1);
            int num2 = Integer.parseInt(numeric2);
            if(num1>num2){
             return num1;
            }
            return num1 - num2 ;
          }
    };
    不知道对大家有啥帮助没,可以讨论下