sorting - Sort the column in a CSV File in Java -


i have csv file reads this.

city,job,salary delhi,doctors,500 delhi,lawyers,400 delhi,plumbers,100 london,doctors,800 london,lawyers,700 london,plumbers,300 tokyo,doctors,900 tokyo,lawyers,800 tokyo,plumbers,400 lawyers,doctors,300 lawyers,lawyers,400 lawyers,plumbers,500 hong kong,doctors,1800 hong kong,lawyers,1100 hong kong,plumbers,1000 moscow,doctors,300 moscow,lawyers,200 moscow,plumbers,100 berlin,doctors,800 berlin,plumbers,900 paris,doctors,900 paris,lawyers,800 paris,plumbers,500 paris,dog catchers,400` 

now, want sort column of salary , write txt file.

i can access column, not able sort it. new csv reading part of java. can help! how should store each salary value variable.

import java.io.*;  public class main {      public static void main(string args[]) throws filenotfoundexception     {         string csv="c:\\users\\dipayan\\desktop\\salaries.csv";         bufferedreader br= new bufferedreader(new filereader(csv));         string line="";          try {             br.readline();              while((line = br.readline())!=null)             {                 string[] f=line.split(",");                 system.out.println(" salary ="+f[2]);             }         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }     } } 

i think should use map here or may

list<integer, string>  

in case of map

 map<integer,string> 

where key salary , string whole complete row. let me write code you.

 map<integer,string> datamap = new hashmap<integer,string>();  string row = br.readline();  string columns[]=row.split(","); // columns[2] has salary  datamap.add(integer.valueof(columns[2]),row); 

use bubble sort saw content or customer compare class sort map. check possible sort functions of collection api.

you can use list<integer,string> 

sort respect integer, keep placing elements on actual positions , re-generate file.

edit: can use tree map since sorts data. use treemap of integer key , string value. put values in , iterate on it, that's all!


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -