ArrayList<Student> list=new ArrayList<Student>();
Student a=new Student("胖胖",20,90.5);
Student b=new Student("刘静",20,92.5);
Student c=new Student("苏凯",20,80.5);
Student d=new Student("许三多",20,90.5);
Student e=new Student("成才",20,90.5);
list.add(a);
list.add(b);
list.add(c);
list.add(d);
list.add(e);
按成绩排序,从高到低

解决方案 »

  1.   

    import java.util.ArrayList;
    import java.util.Collections;
    public class StudentRanging {
        public static void main(String[] args) {
            ArrayList<Student> list=new ArrayList<Student>();
            Student a=new Student("胖胖",20,90.5);
            Student b=new Student("刘静",20,92.5);
            Student c=new Student("苏凯",20,80.5);
            Student d=new Student("许三多",20,90.5);
            Student e=new Student("成才",20,90.5);
            list.add(a);
            list.add(a);
            list.add(b);
            list.add(c);
            list.add(d);
            list.add(e);
            Collections.sort(list);
            for(Student s:list){
                System.out.println("info:"+s);
            }
        }}class Student implements Comparable{
        public String getName() {
            return name;
        }    public void setName(String name) {
            this.name = name;
        }    @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", score=" + score +
                    '}';
        }    public int getAge() {
            return age;
        }    public void setAge(int age) {
            this.age = age;
        }    public double getScore() {
            return score;
        }    public void setScore(double score) {
            this.score = score;
        }    private String name;
        private int age;    Student(String name, int age, double score) {
            this.name = name;
            this.age = age;
            this.score = score;
        }    private double score;
        @Override
        /**
         * 实现按照成绩降序排序
         */
        public int compareTo(Object o) {
            Student s=(Student)o;
            int result=0;
            if(this.score>s.getScore())
               result=-1;
            if(this.score<s.getScore())
               result=1;
            return result; 
        }
    }
      

  2.   


    @Override
        /**
         * 实现按照成绩降序排序
         */
        public int compareTo(Object o) {
            Student s=(Student)o;
            return s.getScore() - this.score
            
        }
      

  3.   

    这样应该更简单点:
    public static void main(String[] args) {
        ArrayList<Student> list=new ArrayList<Student>();
        Student a=new Student("胖胖",20,90.5);
        Student b=new Student("刘静",20,92.5);
        Student c=new Student("苏凯",20,80.5);
        Student d=new Student("许三多",20,90.5);
        Student e=new Student("成才",20,90.5);
        list.add(a);
        list.add(a);
        list.add(b);
        list.add(c);
        list.add(d);
        list.add(e);
        Collections.sort(list, new Comparator(){
    public int compare(Object o1, Object o2) {
    if(((Student)o1).getGread()>((Student)o2).getGread()){
    return 0;
    }else{
    return 1;
    }
    }
        
        });
        for(Student s:list){
         System.out.println(s.getName());
        }
    }
      

  4.   


    public static void main(String[] args) {
      ArrayList<Student> list=new ArrayList<Student>();
      Student a=new Student("胖胖",20,90.5);
      Student b=new Student("刘静",20,92.5);
      Student c=new Student("苏凯",20,80.5);
      Student d=new Student("许三多",20,90.5);
      Student e=new Student("成才",20,90.5);
      list.add(a);
      list.add(a);
      list.add(b);
      list.add(c);
      list.add(d);
      list.add(e);
      Collections.sort(list, new Comparator(){
    public int compare(Object o1, Object o2) {
    if(((Student)o1).getGread()>((Student)o2).getGread()){
    return 0;
    }else{
    return 1;
    }
    }
      
      });
      for(Student s:list){
      System.out.println(s.getName());
      }
    }这样看会好点
      

  5.   

    用比较器就行了:package com.codetest.test.students;import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    import java.util.Comparator;public class SortTest {
    public static void main(String[] args) {
    List<Student> list = new ArrayList<Student>();
    Student a = new Student("胖胖", 20, 90.5);
    Student b = new Student("刘静", 20, 92.5);
    Student c = new Student("苏凯", 20, 80.5);
    Student d = new Student("许三多", 20, 90.5);
    Student e = new Student("成才", 20, 90.5);
    list.add(a);
    list.add(b);
    list.add(c);
    list.add(d);
    list.add(e); Collections.sort(list, new Comparator<Student>() {
    public int compare(Student s1, Student s2) {
    return (s1.getMark() > s2.getMark() ? -1 : s1.getMark() < s2
    .getMark() ? 1 : 0);
    }
    }); for (Student s : list) {
    System.out.println(s.getName() + "\t" + s.getMark());
    }
    }
    }class Student {
    private String name; // 姓名 private int age; // 年龄 private double ; // 分数 public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public int getAge() {
    return age;
    } public void setAge(int age) {
    this.age = age > 0 && age < 100 ? age : 18;
    } public double getMark() {
    return ;
    } public void setMark(double ) {
    this. = ;
    } public Student() {
    } public Student(String name, int age, double ) {
    this.name = name;
    this.setAge(age);
    this. = ;
    }
    }
      

  6.   


     public int compareTo(Object o) {
            Student s=(Student)o;
            return (int)s.getScore() - this.score
            
        }