如何根据最后一列的最大值对多个字符串的数组列表进行排序?

时间:2019-07-18 07:40:45

标签: java arrays sorting

我已经从double类型的文本文件中打印了数据,并将其转换为如下所示的double数组

[-2.0, -2.0, -2.0, 2.0, 2.0, 2.0, 2.0, 1.39E-04 
-2.0, 0.0, -2.0, -2.0, 0.0, -2.0, 2.0, 0.020446 
0.0, -2.0, 2.0, 2.0, -2.0, -2.0, -2.0, 0.032339 
2.0, -2.0, -2.0, 2.0, 2.0, -2.0, 2.0, 0.026673 
0.0, -2.0, -2.0, 0.0, 2.0, 0.0, 2.0, 0.094135 
0.0, 0.0, -2.0, 0.0, 2.0, 2.0, 0.0, 0.045922 
-2.0, 0.0, -2.0, 0.0, 2.0, 0.0, -2.0, 0.117043 
-2.0, -2.0, 2.0, 2.0, 2.0, 2.0, -2.0, 0.425709 
-2.0, -2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 0.156286]

最后一列是根据特定公式计算的每一行的分数。我的问题是如何根据最后一列的最大值对该数组列表排序? 排序后的数组的输出应如下所示

-2.0, -2.0, -2.0, 2.0, 2.0, 2.0, 2.0, 1.39E-04 
-2.0, -2.0, 2.0, 2.0, 2.0, 2.0, -2.0, 0.425709 
-2.0, -2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 0.156286 
-2.0, 0.0, -2.0, 0.0, 2.0, 0.0, -2.0, 0.117043 
0.0, -2.0, -2.0, 0.0, 2.0, 0.0, 2.0, 0.094135 
0.0, 0.0, -2.0, 0.0, 2.0, 2.0, 0.0, 0.045922 
0.0, -2.0, 2.0, 2.0, -2.0, -2.0, -2.0, 0.032339 
2.0, -2.0, -2.0, 2.0, 2.0, -2.0, 2.0, 0.026673 
-2.0, 0.0, -2.0, -2.0, 0.0, -2.0, 2.0, 0.020446 

到目前为止我尝试过的代码

ArrayList<String> a2 = new ArrayList<String>();
               File file7 = new File("kk.txt");
                  BufferedWriter output7 = new BufferedWriter(new FileWriter(file7));
                  output7.write(array+"");
                  output7.close();
                  Scanner s = new Scanner(new FileReader("kk.txt"));
            while (s.hasNextLine()) {
                String line1 = s.nextLine();
                //store this line to string [] here
               line1 = line1.replaceAll("\\[", "");
               line1 = line1.replaceAll("\\]", "");
               line1= line1.replaceAll("\\;,","\r\n"+"");
               line1= line1.replaceAll("\\;","\r\n"+"");
                a2.add(line1);

            }
           Collections.sort(a2,Collections.reverseOrder());
              System.out.println("Sorted List : " + a2);

,但是代码返回数组而不进行排序。感谢您的帮助

2 个答案:

答案 0 :(得分:1)

以下是您遇到的问题的可能解决方案之一:

List<String> a2 = ...; //your list
        Collections.sort(a2, 
                    Collections.reverseOrder((s1, s2) -> {
                        String[] d1 = s1.split(",");
                        String[] d2 = s2.split(",");
                        return Double.compare(Double.parseDouble(d1[d1.length-1]), 
                                              Double.parseDouble(d2[d2.length-1]));
                    }));

a2.forEach(System.out::println);



对于Java 1.7:

Collections.sort(a2, 
                    Collections.reverseOrder(new Comparator<String>() {
                        @Override
                        public int compare(String s1, String s2) {
                            String[] d1 = s1.split(",");
                            String[] d2 = s2.split(",");
                            return Double.compare(Double.parseDouble(d1[d1.length-1]), 
                                                  Double.parseDouble(d2[d2.length-1]));
                        }
                    }));

输出:

-2.0, -2.0, 2.0, 2.0, 2.0, 2.0, -2.0, 0.425709
-2.0, -2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 0.156286
-2.0, 0.0, -2.0, 0.0, 2.0, 0.0, -2.0, 0.117043
0.0, -2.0, -2.0, 0.0, 2.0, 0.0, 2.0, 0.094135
0.0, 0.0, -2.0, 0.0, 2.0, 2.0, 0.0, 0.045922
0.0, -2.0, 2.0, 2.0, -2.0, -2.0, -2.0, 0.032339
2.0, -2.0, -2.0, 2.0, 2.0, -2.0, 2.0, 0.026673
-2.0, 0.0, -2.0, -2.0, 0.0, -2.0, 2.0, 0.020446
-2.0, -2.0, -2.0, 2.0, 2.0, 2.0, 2.0, 1.39E-04

注意1.39E-04实际上是Scientific Notation,其值为0.000139。因此,它似乎位于列表的末尾。

答案 1 :(得分:0)

    Comparator<String> cmp = (o1, o2) -> {
        final Double a1Double = Double.valueOf(o1.substring(o1.lastIndexOf(" ")));
        final Double a2Double = Double.valueOf(o2.substring(o2.lastIndexOf(" ")));
        return a1Double < a2Double ? 0 : -1;

    };