【问题标题】:Sorting a string of numbers with negative integers用负整数对一串数字进行排序
【发布时间】:2013-04-11 02:33:27
【问题描述】:

这两天查了很多,没成功,

现在我有 7 个整数的字符串(+ 和 - ),用逗号分隔。

我已经写了一个示例代码来解释。

        ArrayList<String> str = new ArrayList<String>();

        str.add("9,-9,21,23,28,29,35");
        str.add("18,18,-21,28,28,32,34");
        str.add("-11,-11,22,28,29,-30,31");
        str.add("8,-8,26,31,31,31,31");
        str.add("8,8,26,-32,25,29,35");
        str.add("10,9,-21,45,25,29,35");
        str.add("-11,59,21,25,25,-29,35");
        str.add("12,-9,21,55,25,29,15");
        str.add("9,9,21,25,25,-29,35");
        str.add("7,9,21,25,-35,25,35");
        str.add("4,-39,21,-15,25,-29,35");
        str.add("9,9,21,25,27,29,-35");
        str.add("10,9,21,35,25,39,15");
        str.add("8,-9,21,-25,25,29,-35");
        str.add("18,-9,21,-23,25,29,-35");

        Collections.sort(str);

这不会返回正确的排序数组。它使用数字的第一位进行测试并继续进行排序。

但我想要的是,排序必须基于字符串中的第一个数字。只有当数字相同时(比如字符串数组的第一个数字中有三个 9),它才应该检查这些数字中的第二个数字(单独绑定的字符串)并相应地排序,依此类推。

结果应该是

9 , -9 , 21 , 23 , 28 , 29 , 35
9 , 9 , 21 , 25 , 25 , -29 , 35
9 , 9 , 21 , 25 , 27 , 29 , -35

有没有什么方法可以在这个方法中排序。如果有的话请告诉我,欢迎任何相关的答案。

提前致谢。

【问题讨论】:

  • 您要对ArrayList还是ArrayList中的元素进行排序,请说清楚...
  • 我想对数组列表进行排序,而不是对列表中的元素进行排序。

标签: java algorithm sorting


【解决方案1】:

您为所需的排序语义使用了不正确的数据类型。 Java 看到您想要对字符串进行排序,因此它会按字典顺序对它们进行排序,因为您没有告诉它要这样做。 Java 不是读心者 :)

不要尝试对字符串进行排序,如 "9,-9,21,23,28,29,35",而是对整数数组进行排序,如 {9, 9, -9, 21, 23, 28, 29, 35}。您仍然需要为比较器编写自己的逻辑,但现在相对容易,因为您不必进行任何字符串解析。

如果您需要排序的数据以字符串格式到达您的程序,请在',' 上尝试split,然后将字符串数组的每个组件解析为一个int,最后将其全部转储到一个int 数组或ArrayList 中。

【讨论】:

    【解决方案2】:

    将此比较方法用作sort(List list, Comparator c)的比较器:

    Collections.sort(str, new Comparator<String>(){                    
        public int compare(String str1, String str2){
          int result = 0;
          int i = 0;
          String [] s1 = str1.split(",");
          String [] s2 = str2.split(",");
          while ((i < s1.length) && (i < s2.length) && (result == 0)){
            result = (Integer.parseInt(s1[i]) - Integer.parseInt(s2[i]));
            i++;        
          }
          return (result);
        }
    });
    

    【讨论】:

    • 感谢您的回答,但这是一个无限循环。
    • @Vignesh 抱歉,您必须增加 i。刚刚修好了
    【解决方案3】:

    编写您的自定义排序逻辑并通过Collection#sort

    Collections.sort(str, new Comparator<String>(){
                              public int compare(String str1, String str2){
                                   // Write your logic
                                   // @return a negative integer, zero, or a  
                                  // positive integer as the first argument is less 
                                  //  than, equal to, or greater than the second.
                              }
                          });
    

    【讨论】:

      【解决方案4】:

      您可以创建一个新类,比如 NumberListString 实现 Comparable 接口,并将此字符串作为私有字段。提供适当的构造函数以及访问器/修改器。

      现在重写 compareTo 方法并提供您的比较逻辑。

      然后使用 Collections.sort(str) 进行排序。这将以所需的方式对您的列表进行排序。

      或者,您可以动态创建一个匿名比较器并将其提供给您的 Collections.sort 方法。

      注意:我会推荐第一种方法,因为它允许您抽象出您可能需要对这种特定类型的字符串执行的其他操作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-30
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        • 2010-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多