我读了文件,其中每一行看起来是这样的: userName
,age
,award
排序按索引的Java
我需要编程使两个排序类型:by userName
和by age
。我对第一个没有任何问题,但我不知道如何在这样的例子中按年龄排序.. 我试着在每一行中用年龄切换名称,但它不起作用。 这就是我所(它基本上只是从文件中读取并显示它):
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){
}
任何想法?
我已经试过这一点,但没有奏效:
for(int b=0;b<tabUsers.length-1;b++){
String eachLine =tabUsers[b];
String [] splitLine=eachLine.split(",");
splitLine[0]=splitLine[1];
}
你是什么意思“我知道按姓名排序......但我不按年龄排序”。我只是没有看到它在'String'上对_alphabetically_进行排序所产生的差异,或者只是通过'int'上的自然顺序关系来进行排序......?你能说出你在做什么吗? – 2015-04-01 11:38:30
至于名字我简单地使用这行:Arrays.sort(tabUsers,0,tabUsers.length-1); – chatteVerte 2015-04-01 11:43:28
您是否尝试过分割每一行并获取它们的年龄?如果你有它,你可以用它来排序,对吧? – 2015-04-01 11:48:47