Sort one column of data in a csv file in ascending order in java

16,108

Remove the new lines from 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
    }
}
Share:
16,108
Izzy
Author by

Izzy

Updated on June 27, 2022

Comments

  • Izzy
    Izzy almost 2 years
    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

    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

    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

  • Izzy
    Izzy over 14 years
    yey that works but for some reason it has made it a big block of results, like this: Ab004ui, g46PRo, 54, cb004ui, g46GRo, 94. rather then each result on indiviudal lines. so it should return it like: ab004ui, g46PRo, 34 cd004ui, G46PRo, 29
  • Francis Upton IV
    Francis Upton IV over 14 years
    You might want to repost your code. The code that's posted does seem to be writing a newline at the end of each row. Check that that's still there.
  • Izzy
    Izzy over 14 years
    I edited my original post and added what you suggested, the comma. but not trim() string function, as I keep on getting error. I think I am not adding it right to the code. Also Map is overwriting the userID of the same type which is annoying, is anyway of stopping it from doing that?
  • Izzy
    Izzy over 14 years
    Thank you kindly for this, I have marked this as the answer, I am acutally going to get some reading done on maps, as this is a very interesting way of completing this problem. Now all I have to do is make it into a method and im done. Thanks again for all your help and explanations