你的txt文件是干吗的啊?是要从中用java读取的,还是作为资料。
如果作为资料的话倒好写,就是写个实体类,对实体类的数学成绩排序
测试类:
public static void main(String[] args) {
Course c1 = new Course("小明",90,80);
Course c2 = new Course("小红",80,95);
Course c3 = new Course("小亮",101,79);
List
list.add(c1);
list.add(c2);
list.add(c3);
Collections.sort(list);
for(int i=0;i
}
}
实体类:
public class Course implements Comparable{
private Integer math ;
private Integer english;
private String name;
public Course(String name,Integer math,Integer english){
this.english = english;
this.name = name;
this.math = math;
}
public Integer getMath() {
return math;
}
public void setMath(Integer math) {
this.math = math;
}
public Integer getEnglish() {
return english;
}
public void setEnglish(Integer english) {
this.english = english;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(Object o) {
Course c = (Course)o;
if(this.math > c.math){
return -1;
}else if(this.math == c.math){
return 0;
}else{
return 1;
}
}
}
如果要用java读取的话,先通过正则取出值,再进行排序,主要是在测试类中进行,其他的不变
很明显你的文本格式为:
字符串1+空格+数学成绩+空格+字符串2+空格+英语成绩。
所以:
String str="小明数学 90 小明英语 80";
String [] ss=str.split(" ");就可以得到 字符串1 数学成绩 字符串2 英语成绩 这四个字符。
之后用Double.valueOf("数学成绩“)就可以得数学成绩。。
故而,可以构建一个学生的类,实现Comparable接口。学生类属性为String inputStr,double mathScore;compareTo方法就是根据mathScore来比较即可
用Hashmap存储数据,再先按数学排序,再按英语排序