【问题标题】:Java: Sort a String array in a numeric wayJava:以数字方式对字符串数组进行排序
【发布时间】:2011-11-12 23:25:45
【问题描述】:

我有一个包含以下条目的字符串数组:

Array[0] = "70% Marc"
Array[1] = "50% Marc"
Array[2] = "100% Marc"
Array[3] = "20% Marc"

我想对这个数组进行降序排序。 当我使用Arrays.sort(Array) 时,它会按降序排序,但100% Marc 位于底部(因为它只查看第一个字符来对其进行排序)。我希望它像这样排序:

"100% Marc"
"70% Marc"
"50% Marc"
"20% Marc"

我该怎么做?

【问题讨论】:

    标签: java arrays string sorting


    【解决方案1】:

    您将需要一个自定义比较器:

    import java.util.Comparator;
    
    public class CustomComparator implements Comparator<String>{
    
        @Override
        public int compare(String firstString, String secondString) {
            int number1 = Integer.parseInt(firstString.substring(0, firstString.indexOf('%') - 1);
            int number2 = Integer.parseInt(secondString.substring(0, secondString.indexOf('%') - 1);
            return number1.compareTo(number2);
        }
    }
    

    将此比较器与以下内容一起使用:

    List<String> entries = new ArrayList<String>();
    entries.add("70% Marc");
    entries.add("50% Marc");
    entries.add("100% Marc");
    entries.add("20% Marc");
    
    CustomComparator comparator = new CustomComparator();
    Collections.sort(entries, comparator);
    

    【讨论】:

      【解决方案2】:

      编写您自己的CustomStringComparator 并将其与排序方法一起使用。

      public class CustomStringComparator implements Comparator<String>{
      
          @Override
          public int compare(String str1, String str2) {
      
             // extract numeric portion out of the string and convert them to int
             // and compare them, roughly something like this
      
             int num1 = Integer.parseInt(str1.substring(0, str1.indexOf("%") - 1));
             int num2 = Integer.parseInt(str2.substring(0, str2.indexOf("%") - 1));
      
             return num1 - num2;
      
          }
      }
      

      【讨论】:

        【解决方案3】:

        您需要实现一个自定义Comparator&lt;String&gt;,通过删除百分号来处理比较:

        public int compare(String str1, String str2) {
            Integer number1 = Integer.parseInt(str1.substring(0, str1.length - 1));
            Integer number2 = Integer.parseInt(str1.substring(0, str2.length - 1));
            return number1.compareTo(number2);
            // or use primitives, and then: return (x < y) ? -1 : ((x == y) ? 0 : 1);
            // but that's harder to read
        }
        

        比较本身可以通过使用Integer 包装器、我粘贴的代码(取自包装器)或番石榴的Ints.compare(int1, int2) 来完成

        然后使用Collections.sort(array, comparator)

        【讨论】:

          【解决方案4】:

          它不是查看第一个字符,它是根据字符串值排序的,因为这就是您的数组中的内容。

          我建议存储两个字段,

          数组[0,0] = 50; Array[0,1] = "马克"

          然后根据数字字段进行排序,如果这是您所追求的行为。

          【讨论】:

            【解决方案5】:

            您必须使用自定义Comparator。基本上在您的方法compare() 中,您将编写订购两个Strings 的逻辑。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2018-09-28
              • 2012-10-14
              • 2021-03-21
              • 1970-01-01
              • 2020-11-16
              • 1970-01-01
              • 2017-06-07
              相关资源
              最近更新 更多