简体   繁体   中英

How to sort an arraylist of multiple strings based on the highest value of the last column?

I have printed data from a text file of type double and converting it into an array of double that looks like this

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

The last column is the score of each row that calculated based on specific equation. My question is how can I sort this arraylist based on the highest value of the last column? The output of the sorted array should be like this

-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 

the code I tried so far

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

but the code return the array without sorting. Any help is appreciated

Here's one of possible solutions for your problem:

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



For 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]));
                        }
                    }));

Output:

-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

NOTE : 1.39E-04 is actually a Scientific Notation and its value is 0.000139 . That's why it appears to be at the end of the list.

    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;

    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM