gpt4 book ai didi

java - 如何排序 List>

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:11:27 26 4
gpt4 key购买 nike

我有一项任务是根据一些要求对文本文件进行排序:

  1. 根据列对行进行排序
  2. 按第一列对行进行排序(如果列中的数据相同则按第二列排序)等等。排序后行中的数据保持不变;
  3. 数字必须按升序排列,字母必须按字母顺序排列,数字高于字母
  4. 用制表符分隔的列 ("\t")

我做了什么:读取文件并将所有内容复制到 List> 中,其中 List 的每个元素都是来自存储在 List 中的文件的行,这里是代码:

public class ReadDataFile {
public static List<List<String>> readData(String fileName) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fileName + ".txt"));
List<List<String>> data = new ArrayList<List<String>>();
String line;
while (true) {
line = br.readLine();
if (line == null)
break;
List<String>lines = Arrays.asList(line.split("\t"));
data.add(lines);
System.out.println(lines);
}
br.close();
return data;

并将数据写入另一个文件:

    public void writeToFile(String fileName) throws IOException {
FileWriter writer = new FileWriter(fileName);
List<List<String>> data = ReadDataFile.readData("input");

Collections.sort(data, new Comparator<List<String>>() {
@Override
public int compare(List<String> o1, List<String> o2) {
// TODO Auto-generated method stub
return o1.get(0).compareTo(o2.get(0));
}
});

for (List<String> lines : data) {
for (int i = 0; i < lines.size(); i++) {
writer.write(lines.get(i));
if (i < lines.size() - 1) {
writer.write("\t");
}
}
writer.write("\n");

}
writer.close();
}

问题是:

public int compare(List<String> o1, List<String> o2) {
// TODO Auto-generated method stub
return o1.get(0).compareTo(o2.get(0));
}

没有正确排序我需要的。

输入文件示例:

-2.2 2 3 4 329 2
2.2 12345q 69 -afg
2.2 12345q 69 -asdf
-22 1234234 asdfasf asdgas
-22 11 abc
-22 -3 4
-1.1
qqqq 1.1

最终预期输出是:

-22 -3 4
-22 11 abc
-22 1234234 asdfasf asdgas
-2.2 2 3 4 329 2
-1.1
2.2 12345q 69 -afg
2.2 12345q 69 -asdf
qqqq 1.1

但是,我得到的是:

-1.1
-2.2 2 3 4 329 2
-22 -3 4
-22 11 abc
-22 1234234 asdfasf asdgas
2.2 12345q 69 -afg
2.2 12345q 69 -asdf
qqqq 1.1

问题是,如何写一个合适的排序?谢谢解答

最佳答案

您似乎希望使用数字比较对作为有效数字的字符串值进行排序。由于您的示例包含非整数值,您可以选择使用 doubleBigDecimal 进行数字比较。下面的代码使用 BigDecimal因此可以比较任何大小的数字,而不会损失精度,但它不支持 "Infinite""-Infinite" 的特殊值>"NaN",或 Double.parseDouble()HexFloatingPointLiteral 格式支持。

将数字与字符串进行比较应该将数字排在字符串之前。

为了比较字符串与字符串,您可以对 lexicographically 进行排序,不区分大小写,或使用 Collator用于语言环境敏感的比较。下面的代码使用 Collat​​or 作为默认语言环境。

比较将比较列表的第一个值,如果相等则比较第二个值,依此类推。如果一个列表较短,并且列表在该点之前相等,则较短的列表首先排序。

public final class NumberStringComparator implements Comparator<List<String>> {
private Collator collator = Collator.getInstance();
@Override
public int compare(List<String> r1, List<String> r2) {
for (int i = 0; ; i++) {
if (i == r1.size())
return (i == r2.size() ? 0 : -1);
if (i == r2.size())
return 1;
String v1 = r1.get(i), v2 = r2.get(i);
BigDecimal n1 = null, n2 = null;
try { n1 = new BigDecimal(v1); } catch (@SuppressWarnings("unused") NumberFormatException unused) {/**/}
try { n2 = new BigDecimal(v2); } catch (@SuppressWarnings("unused") NumberFormatException unused) {/**/}
int cmp = (n1 == null ? (n2 == null ? this.collator.compare(v1, v2) : 1) : (n2 == null ? -1 : n1.compareTo(n2)));
if (cmp != 0)
return cmp;
}
}
}

测试

String input = "-2.2\t2\t3\t4\t329\t2\n" +
"2.2\t12345q\t69\t-afg\n" +
"2.2\t12345q\t69\t-asdf\n" +
"-22\t1234234\tasdfasf\tasdgas\n" +
"-22\t11\tabc\n" +
"-22\t-3\t4\n" +
"-1.1\n" +
"qqqq\t1.1";
List<List<String>> data = new ArrayList<>();
try (BufferedReader in = new BufferedReader(new StringReader(input))) {
for (String line; (line = in.readLine()) != null; )
data.add(Arrays.asList(line.split("\t")));
}
data.sort(new NumberStringComparator());
data.forEach(System.out::println);

输出

[-22, -3, 4]
[-22, 11, abc]
[-22, 1234234, asdfasf, asdgas]
[-2.2, 2, 3, 4, 329, 2]
[-1.1]
[2.2, 12345q, 69, -afg]
[2.2, 12345q, 69, -asdf]
[qqqq, 1.1]

关于java - 如何排序 List<List<String>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44312267/

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