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

import*; import java.util.*; public class Sort { public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new FileReader("data1.csv")); Map map=new TreeMap(); 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
Be careful, that if you use a Map, if you have duplicate keys, they will be overwritten and you will lose them.

以上就是Sort one column of data in a csv file in ascending order in java的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准