简体   繁体   English

排序包含数字的字符串

[英]Sort strings that contains number

I have list of strings that contains numbers: 我有一个包含数字的字符串列表:

Example: 例:

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

I need to sort this list and get this: 我需要对这个列表进行排序并得到这个:

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

But if i use java functions Collections.sort i get this: 但是,如果我使用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

Edit: 编辑:

I've tried with this comparator: 我已经尝试过使用此比较器:

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;
}

When i run app i get this error java.lang.NumberFormatException: For input string: "Unknown" how can i check is there strings that doesn't contains number so that i can test this comparator? 当我运行应用程序时,出现此错误java.lang.NumberFormatException: For input string: "Unknown" ,我如何检查是否存在不包含数字的字符串,以便我可以测试此比较器?

you can get the numeric value of those strings and use java8 streams 您可以获取这些字符串的数值并使用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);

Instead you can use a Comparator like this : 相反,您可以使用以下比较器:

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);

Outputs 输出

[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]

The idea is : 这个想法是:

  1. replace all . 全部替换. with empty 空着
  2. convert your number to the correct number 将您的电话号码转换为正确的电话号码
  3. compare the result like any number. 像任何数字一样比较结果。

you can write a comparator like: 您可以编写一个比较器,例如:

@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;
}

And then just: 然后:

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

Hope it Helps! 希望能帮助到你!

You need to tokenize them, and following compare from the most significant token to the end. 您需要标记它们,然后从最重要的标记到最后进行比较。 as an example You can do as follow to compare two sequence a and b. 例如,您可以按照以下步骤比较两个序列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;
    }

from this snippet you can build your comparator hope this help 从这个片段中,您可以构建您的比较器,希望对您有所帮助

I've solved my problem wirth this comparator: 我已经解决了这个比较器的问题:

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.

相关问题 如何在Java中对字符串进行排序以包含数字 - How to sort strings contains numbers in java 使用 Comparator 对 String 包含 4 个数字的字符串列表进行排序 - Sort the list of Strings where String contains 4 numbers, by using Comparator 按对象列表(包含字符串列表)之一对列表进行排序 - Sort a List of objects by one of it`s fields (that contains list of Strings) Java-对字符串列表进行排序,根据字符串的内容确定顺序 - Java - Sort a List of strings, determining the order depending on the contains of the string 在Java中按单词的索引号对字符串进行排序(数字作为参数给出) - Sort strings by index number of word (the number is given as an argument) in java 给定数字N和N个字符串数组,请基于每个字符串中的元音数量以降序对字符串进行排序 - Given a number N and an array of N strings, sort the strings based on number of vowels in each of the strings in the descending order 订购包含String的Map,但是字符串可能代表一个数字 - Order a Map that contains String, but strings might represent a number 如何对字符串中包含字母数字字符的字符串进行排序,例如 ROLL-10 &gt; ROLL-2 - How to sort strings where a string contains alpha numeric characters like ROLL-10 > ROLL-2 文本文件包含字符串和数字。 如何找到与字符串关联的最大数字? - Text file contains strings and numbers. How do I find the largest number associated with the string? 如何从.txt文件中读取字符串,然后根据出现的次数将它们分类到ArrayList中? - How to read strings off of .txt file and sort them into an ArrayList based on the number of occurrences?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM