在java中按升序对csv文件中的一列数据排序 [英] Sort one column of data in a csv file in ascending order in java

查看:740
本文介绍了在java中按升序对csv文件中的一列数据排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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
我试图读取一个未排序的文件,排序csv数据文件的一列,并将这些结果打印在一个新文件中。我在这个网站上搜索时借了这个解决方案,因为我认为这是我想要实现的理想。我有一个282行的数据形式为

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

UserID, Module, Mark

Ab004ui, g46PRo, 54

cb004ui, g46GRo, 94

gy004ui, g46GRo, 12

ab004ui, g46PRo, 34

这是在csv文件中。
当我使用上面的代码它只给我一行在sorted_marks.txt,像这样

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

ab004ui, g46PRo, 34

,我相信它不排序。

我想从新文件的所有结果,基于他们的userID排序,没有别的,但我似乎无法得到它的工作,请任何帮助将是巨大的

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

推荐答案

从data1.csv中删除新行。

Remove the new lines from data1.csv.

我希望使用第二个通用String作为字符串列表,一切都几乎相同,如下所示

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
    }
}

这篇关于在java中按升序对csv文件中的一列数据排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆