java 在java中按升序对csv文件中的一列数据进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1894060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sort one column of data in a csv file in ascending order in java
提问by Izzy
import java.io.*;
import java.util.*;
public class Sort {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new FileReader("data1.csv"));
Map<String, String> map=new TreeMap<String, String>();
String line="";
while((line=reader.readLine())!=null){
map.put(getField(line),line);
}
reader.close();
FileWriter writer = new FileWriter("sorted_numbers.txt");
for(String val : map.values()){
writer.write(val);
writer.write('\n');
}
writer.close();
}
private static String getField(String line) {
return line.split(",")[0];//extract value you want to sort on
}
}
Hia I am trying to read a unsorted file and get Java to sort one column of the csv data file and print those results in a new file. I borrowed this solution whilst I was searching on this website because I think it is ideal for what I am trying to acomplish. I have a 282 rows of data in the form of
您好,我正在尝试读取未排序的文件并让 Java 对 csv 数据文件的一列进行排序,并将这些结果打印在一个新文件中。我在这个网站上搜索时借用了这个解决方案,因为我认为它是我想要完成的理想选择。我有 282 行数据的形式
UserID, Module, Mark
Ab004ui, g46PRo, 54
cb004ui, g46GRo, 94
gy004ui, g46GRo, 12
ab004ui, g46PRo, 34
this is in the csv file. when I use the above code it only gives me one line in the sorted_marks.txt, like this
这是在 csv 文件中。当我使用上面的代码时,它只在 sorted_marks.txt 中给我一行,就像这样
ab004ui, g46PRo, 34
and I believe it wasnt even sorted.
我相信它甚至没有排序。
I want all the results from the new file to be sorted based on their userID and nothing else but I can't seem to get it to work, please any help would be greatful
我希望新文件的所有结果都根据他们的用户 ID 进行排序,而不是其他任何东西,但我似乎无法让它工作,请提供任何帮助
回答by DKSRathore
Remove the new lines from data1.csv.
从 data1.csv 中删除新行。
I would prefer to use the second generic String of the Map as a list of string and everything is almost same like below
我更愿意使用地图的第二个通用字符串作为字符串列表,一切都几乎相同,如下所示
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class Sort {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new FileReader("data1.csv"));
Map<String, List<String>> map = new TreeMap<String, List<String>>();
String line = reader.readLine();//read header
while ((line = reader.readLine()) != null) {
String key = getField(line);
List<String> l = map.get(key);
if (l == null) {
l = new LinkedList<String>();
map.put(key, l);
}
l.add(line);
}
reader.close();
FileWriter writer = new FileWriter("sorted_numbers.txt");
writer.write("UserID, Module, Mark\n");
for (List<String> list : map.values()) {
for (String val : list) {
writer.write(val);
writer.write("\n");
}
}
writer.close();
}
private static String getField(String line) {
return line.split(",")[0];// extract value you want to sort on
}
}
回答by Francis Upton IV
You probably want line.split(",")[0], not space. But you should also trim the white space from beginning and end of your sort key using the appropriate string function.
您可能需要 line.split(",")[0],而不是空格。但是您还应该使用适当的字符串函数从排序键的开头和结尾修剪空白。

