Sorting by index java -
i read file every line looks this: username
,age
,award
i need program 2 sorting types : by username
, by age
. had no problem first one, have no idea how sort age in such example.. tried switching name age in each line doesn't work. that's have ( it's reading file , displaying ):
try{ file file=new file(path); bufferedreader read=new bufferedreader(new filereader(file)); string line=read.readline(); all+=line+"\n"; while(line!=null){ line=save.readline(); all+=line+"\n"; } string [] tabusers=all.split("\n"); string display=""; for(int a=0;a<tabusers.length-1;a++){ display+=tabusers[a]+"\n"; } for(int c=0;c<tabusers.length-1;c++){ system.out.println(tabusers[c]); } } catch(exception ex){ }
any ideas?
i have tried this, did not work :
for(int b=0;b<tabusers.length-1;b++){ string eachline =tabusers[b]; string [] splitline=eachline.split(","); splitline[0]=splitline[1]; }
first names , ages each line. put things on 2 treemaps, 1 names keys , other age keys. like:
string[] lines = new string[3]; lines[0] = "michael 25 award_1"; lines[1] = "ana 15 award_2"; lines[2] = "bruno 50 award_3"; string[] parts; map<string, string> linesbyname = new treemap<string, string>(); map<integer, string> linesbyage = new treemap<integer, string>(); (string line : lines) { parts = line.split(" "); linesbyname.put(parts[0], line); linesbyage.put(integer.parseint(parts[1]), line); } system.out.println("sorted name:"); (map.entry<string, string> entry : linesbyname.entryset()) { system.out.println(entry.getvalue()); } system.out.println("\nsorted age:"); (map.entry<integer, string> entry : linesbyage.entryset()) { system.out.println(entry.getvalue()); }
Comments
Post a Comment