对象类型List<User>,想根据里面的percent字段排序,percent是字符串类型百分比形如"25.12%",想根据这个字段由高到低排序!

解决方案 »

  1.   

    我这里有个sample
    People.java
    package com.zhoujl.test;/**
     * @author zhoujl
     * 2011/12/28 10:11:04
     */
    public class People implements Comparable { private int age;
    private String name;

    public People(int age,String name) {
    this.age = age;
    this.name = name;
    } public int getAge() {
    return age;
    } public void setAge(int age) {
    this.age = age;
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } /* 
     * 在这里写比较的逻辑return 1就是大于  return 0就是小于
     */
    @Override
    public int compareTo(Object people) {

    if(this.age>((People)people).age) {
    return 1;
    }else { 
    return 0;
    }
    } @Override
    public String toString() {
    return "Name:" +name+"\n"+"Age:"+age+"\n";
    }




    }TestSort.java
    package com.zhoujl.test;import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;/**
     * @author zhoujl
     * 2011/12/28 10:10:47
     */
    public class TestSort { @SuppressWarnings("unchecked")
    public static void main(String[] args) {
    List<People> list = new ArrayList<People>();

    People people1 = new People(12,"zhou");
    People people2 = new People(18,"zho");
    People people3 = new People(12,"zh");
    People people4 = new People(21,"z");
    list.add(people1);
    list.add(people2);
    list.add(people3);
    list.add(people4);
    for(People people:list) {
    System.out.println(people);
    }
    Collections.sort(list);
    System.out.println("--------------------------");
    for(People people:list) {
    System.out.println(people);
    }
    }
    }
    你自己改一下比较大小的逻辑 就可以了 另外还有一种方法也可以实现利用comparator
      

  2.   

    Collections.sort(list, new Comparator<User>(){
            public int compare(User u1, User u2){
                String p1 = u1.getPercent();
                String p2 = u2.getPercent();
                if(p1.equals(p2)) return 0;
                DecimalFormat formatter = NumberFormat.getPercentInstance();
                double d1 = formatter.parse(p1);
                double d2 = formatter.parse(p2);
                if(d1 > d2) return 1;
                return -1;
            }
        });
      

  3.   

    User类需要实现 Compareable接口里面的compareTo方法。
    然后根据Collections.sort进行排序就可以了。
      

  4.   

      double d1 = formatter.parse(p1);
      double d2 = formatter.parse(p2);
    这里转不了 报错是为啥
      

  5.   

      double d1 = formatter.parse(p1);
      double d2 = formatter.parse(p2);
    这里转不了 报错是为啥
      

  6.   

    comparable 或者 comparator
    建议用comparator吧,这个比较独立,不用跟User类绑到一起
      

  7.   

      double d1 = formatter.parse(p1);
      double d2 = formatter.parse(p2);
    这里转不了 报错是为啥
      

  8.   

    formatter.parse(p1) 是Number类型
      

  9.   

    Collections.sort(list, new Comparator<TerminalPerformanceObj>(){
      public int compare(TerminalPerformanceObj u1, TerminalPerformanceObj u2){
      String p1 = u1.getMtSuccPercent();;
      String p2 = u2.getMtSuccPercent();
      if(p1.equals(p2)) return 0;
      DecimalFormat formatter = (DecimalFormat) NumberFormat.getPercentInstance();
      Number d1 = null;
    try {
    d1 = formatter.parse(p1);
    } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
      Number d2 = null;
    try {
    d2 = formatter.parse(p2);
    } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
      if(d1.floatValue() > d2.floatValue()) return 1;
      return -1;
      }
      });
    我改了一下,但是实际结果却是按从小到大的排序,如何能按从大到小排序呢