簡體   English   中英

排序包含數字的字符串

[英]Sort strings that contains number

我有一個包含數字的字符串列表:

例:

String 1.0.2
String 2.1.2
String 10.0.1
String 3.0.1
String 2.3.1
String 10.2.1

我需要對這個列表進行排序並得到這個:

String 1.0.2
String 2.1.2
String 2.3.1
String 3.0.1
String 10.0.1
String 10.2.1

但是,如果我使用Java函數Collections.sort,則會得到以下信息:

String 1.0.2
String 10.0.1
String 10.2.1
String 2.1.2
String 2.3.1
String 3.0.1

編輯:

我已經嘗試過使用此比較器:

public int compare(String o1, String o2) {
    int a = Integer.parseInt(o1.split(" ")[1].replace(".", ""));
    int b = Integer.parseInt(o2.split(" ")[1].replace(".", ""));

    return a-b;
}

當我運行應用程序時,出現此錯誤java.lang.NumberFormatException: For input string: "Unknown" ,我如何檢查是否存在不包含數字的字符串,以便我可以測試此比較器?

您可以獲取這些字符串的數值並使用java8流

List<String> myList = Arrays.asList("String 1.0.2", "String 2.1.2", "String 10.0.1", "String 3.0.1",
            "String 2.3.1", "String 10.2.1");

myList.sort((x, y) -> Integer.compare(Integer.parseInt(x.replace(".", "").split(" ")[1]),
            Integer.parseInt(y.replace(".", "").split(" ")[1])));
System.out.println(myList);

相反,您可以使用以下比較器:

List<String> list = Arrays.asList("1.0.2", "2.1.2", "10.0.1", "3.0.1", "2.3.1", "10.2.1");
System.out.println(list);
Collections.sort(list, (o1, o2) -> {
    return Integer.parseInt(o1.replace(".", "")) - Integer.parseInt(o2.replace(".", ""));
});
System.out.println(list);

輸出

[1.0.2, 2.1.2, 10.0.1, 3.0.1, 2.3.1, 10.2.1]
[1.0.2, 2.1.2, 2.3.1, 3.0.1, 10.0.1, 10.2.1]

這個想法是:

  1. 全部替換. 空着
  2. 將您的電話號碼轉換為正確的電話號碼
  3. 像任何數字一樣比較結果。

您可以編寫一個比較器,例如:

@Override
public int compare(String o1, String o2) {
    int a = Integer.parseInt(o1.split(" ")[1].replace(".", ""));
    int b = Integer.parseInt(o2.split(" ")[1].replace(".", ""));
    return a - b;
}

然后:

CustomComparator comp = new CustomComparator();
Collections.sort(list, comp);

希望能幫助到你!

您需要標記它們,然后從最重要的標記到最后進行比較。 例如,您可以按照以下步驟比較兩個序列a和b。

    public int compare(Sring a,String b){
        String[] aToken = a.slpit(".");
        String[] bToken = b.slpit(".");
        if (aToken.length > bToken.length){
            return 1;
         }
        if (bToken.length > aToken.length){
            return -1;
        }
        for (int i=0; i<bToken.length; i++){
            if (aToken[i].compareTo(b[i]Token) != 0){
               return aToken[i].compareTo(bToken[i]);
           }
       }
      return 0;
    }

從這個片段中,您可以構建您的比較器,希望對您有所幫助

我已經解決了這個比較器的問題:

public int compare(String o1, String o2)
        {
            String a = o1.toString();
            String b = o2.toString();

            int ia = 0, ib = 0;
            int nza = 0, nzb = 0;
            char ca, cb;

            while (true) {
                // Only count the number of zeroes leading the last number compared
                nza = nzb = 0;

                ca = charAt(a, ia);
                cb = charAt(b, ib);

                // skip over leading spaces or zeros
                while (Character.isSpaceChar(ca) || ca == '0') {
                    if (ca == '0') {
                        nza++;
                    } else {
                        // Only count consecutive zeroes
                        nza = 0;
                    }

                    ca = charAt(a, ++ia);
                }

                while (Character.isSpaceChar(cb) || cb == '0') {
                    if (cb == '0') {
                        nzb++;
                    } else {
                        // Only count consecutive zeroes
                        nzb = 0;
                    }

                    cb = charAt(b, ++ib);
                }

                // Process run of digits
                if (Character.isDigit(ca) && Character.isDigit(cb)) {
                    int bias = compareRight(a.substring(ia), b.substring(ib));
                    if (bias != 0) {
                        return bias;
                    }
                }

                if (ca == 0 && cb == 0) {
                    // The strings compare the same. Perhaps the caller
                    // will want to call strcmp to break the tie.
                    return nza - nzb;
                }
                if (ca < cb) {
                    return -1;
                }
                if (ca > cb) {
                    return +1;
                }

                ++ia;
                ++ib;
            }
        }

        int compareRight(String a, String b)
        {
            int bias = 0, ia = 0, ib = 0;

            // The longest run of digits wins. That aside, the greatest
            // value wins, but we can't know that it will until we've scanned
            // both numbers to know that they have the same magnitude, so we
            // remember it in BIAS.
            for (;; ia++, ib++)
            {
                char ca = charAt(a, ia);
                char cb = charAt(b, ib);

                if (!Character.isDigit(ca) && !Character.isDigit(cb)) {
                    return bias;
                }
                if (!Character.isDigit(ca)) {
                    return -1;
                }
                if (!Character.isDigit(cb)) {
                    return +1;
                }
                if (ca == 0 && cb == 0) {
                    return bias;
                }

                if (bias == 0) {
                    if (ca < cb) {
                        bias = -1;
                    } else if (ca > cb) {
                        bias = +1;
                    }
                }
            }
        }

        static char charAt(String s, int i) {
            return i >= s.length() ? 0 : s.charAt(i);
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM