import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Admin {
public static void main(String... args) {
List data = new ArrayList();
Student s0 = new Student();
s0.setNo("000");
s0.setName("a");
s0.setChengji(100.0);
data.add(s0);
Student s1 = new Student();
s1.setNo("001");
s1.setName("b");
s1.setChengji(99.0);
data.add(s1);
Student s2 = new Student();
s2.setNo("002");
s2.setName("c");
s2.setChengji(98.0);
data.add(s2);
System.out.println("排序前");
show(data);
System.out.println("排序后");
sort(data);
show(data);
}
private static void sort(List data) {
Collections.sort(data, new Comparator() {
public int compare(Object o1, Object o2) {
Student s1 = (Student) o1;
Student s2 = (Student) o2;
return (int) (s1.getChengji() - s2.getChengji());
}
});
}
private static void show(List data) {
for (int i = 0; i < data.size(); i++) {
System.out.println(data.get(i));
}
}
}
class Student {
private String no;
private String name;
private double chengji;
public double getChengji() {
return chengji;
}
public void setChengji(double chengji) {
this.chengji = chengji;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String toString() {
return "学号:" + no + ",姓名:" + name + ",成绩:" + chengji;
}
}
结果
排序前
学号:000,姓名:a,成绩:100.0
学号:001,姓名:b,成绩:99.0
学号:002,姓名:c,成绩:98.0
排序后
学号:002,姓名:c,成绩:98.0
学号:001,姓名:b,成绩:99.0
学号:000,姓名:a,成绩:100.0
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Test {
// 存放学生的list
private static List
// 给students里添加数据
public static void init() {
students.add(new Student(10001, "张三", 99));
students.add(new Student(10002, "李四", 67));
students.add(new Student(10003, "王五", 88));
students.add(new Student(10004, "赵六", 44));
students.add(new Student(10005, "朱七", 51));
students.add(new Student(10006, "倩倩", 89));
}
// 对students进行排序
@SuppressWarnings("unchecked")
public static void sort() {
Collections.sort(students, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
int one = ((Student)o1).getScore();
int two = ((Student)o2).getScore();
return one - two;
}
});
}
public static void main(String[] args) {
init(); // 初始化
sort(); // 排序
// 输出显示
for(Student s : students) {
System.out.println("id:" + s.getId() + " name:" + s.getName() + " score:" + s.getScore());
}
}
}
// 学生类
class Student {
private int id;
private String name;
private int score;
public Student(int id, String name, int score) {
setId(id);
setName(name);
setScore(score);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
对于学生类 可以直接声明
public class student(){
private int sno;
private String sname;
private int score;
}
然后set get方法
在action中放入list接入student类
前台列表直接获得list 然后迭代 这个是大体思路
有什么不懂可以追问。
如果是从数据库中查的就好办了!