gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:40:48 28 4
gpt4 key购买 nike

我已经从 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);

但是代码返回数组而不进行排序。任何帮助表示赞赏

最佳答案

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

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。这就是为什么它出现在列表的末尾。

关于java - 如何根据最后一列的最大值对多个字符串的数组列表进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57089423/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com