【问题标题】:Sort multiple fields in lists with null value use Collections.sort() in java?在java中使用Collections.sort()对具有空值的列表中的多个字段进行排序?
【发布时间】:2014-01-16 10:35:40
【问题描述】:

我有一个具有三个属性的学生类:

private String name;
private String age;
private String classStudy;

并实现 Collections Comparable

@Override
public int compareTo(Student o) {
    if(name==null) return -100;
        else if(age==null) return -50;
        else if(classStudy==null) return -10;
    else
        return (name + age + classStudy).compareTo(o.getName() + o.getAge() + o.getClassStudy());
}

主方法:

 public static void main(String []args){
    List<Student> lists = new ArrayList<Student>();
    lists.add(new Student("Tung", "24", "Java"));
    lists.add(new Student("Hong", "26", "Uava"));
    lists.add(new Student("yoiy", "42", "jrva"));
    lists.add(new Student("Tung", "22", "Aava"));
    lists.add(new Student("Tung", null, "Aava"));
    lists.add(new Student("Tyn", "22", "Aava"));
    lists.add(new Student("Tungh", "22", "Aava"));
    lists.add(new Student("aung", "39", "ora"));
    lists.add(new Student(null, null, "Aava"));
    lists.add(new Student("Rung", "17", "Kva"));
    lists.add(new Student(null, null, null));

    Collections.sort(lists);

    for(Student listTemp : lists){
        System.out.println("Name : " + listTemp.getName() + ", Age : " + listTemp.getAge()
                           + ", Class : "+ listTemp.getClassStudy());
    }
}

结果: https://lh6.googleusercontent.com/-IIGbZ4uThRk/Ute08Qt6UJI/AAAAAAAAAsg/ahqgAKgMSHc/w325-h219-no/Capture6.PNG 但我希望 null 值按第一个位置排序(排序跟随名称 -> 年龄 -> 类)。我该怎么做?

【问题讨论】:

  • 所以如果一个名字是空的,你希望它是第一个吗?
  • 您的 compareTo 需要保持一致。 a.compareTo(b) == -b.compareTo(a) 但如果名称在两种情况下都为空,则始终返回 -100。您应该一次比较每个字段,而不是尝试组合它们。
  • 顺便注意,在 compareTo 合约docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html987654322@中返回-1、-50或-100没有区别

标签: java generics null comparator comparable


【解决方案1】:

此方法将使null 值排序到列表的顶部,并按名称、年龄和最后的班级对列表进行排序。它使用了一个小的辅助函数,因为比较函数会经常重复。

@Override
public int compareTo(Student s) {
    int c1 = this.compare(this.name, s.name);
    if (c1 != 0) {
        return c1;
    } else {
        int c2 = this.compare(this.age, s.age);
        if (c2 != 0) {
            return c2;
        } else {
            return this.compare(this.classStudy, s.classStudy);
        }
    }
}

public int compare(String s1, String s2) {
    if (s1 == null && s2 == null) {
        return 0;
    } else if (s1 == null) {
        return -1;
    } else if (s2 == null) {
        return 1;
    } else {
        return s1.compareTo(s2);
    }
}

【讨论】:

  • 谢谢。但我希望 null 值是最高优先级。示例:姓名:Hong,年龄:26,班级:Uava 和姓名:Tung,年龄:null,班级:Aava。所以姓名:Tung,年龄:null,班级:Aava 在姓名:Hong 前面,年龄:26,班级:Uava ???
【解决方案2】:

这样做

@Override
public int compareTo(Student o) {
    if(name==null||o.getName()==null){
         return 1;
    }
    if(!name.equalsIgnoreCase(o.getName())){
         return name.compareToIgnoreCase(o.getName());
    }       
    if(age==null||o.getAge()==null){
         return 1;
    }
    if(!age.equalsIgnoreCase(o.getAge())){
        return age.compareToIgnoreCase(o.getAge());
    }
    if(classStudy==null|| o.getClassStudy()==null){
         return 1;
    }
    if(!classStudy.equalsIgnoreCase(o.getClassStudy())){
         return classStudy.compareToIgnoreCase(o.getClassStudy());
    }
    return 0;
}

【讨论】:

  • 尝试将lists.add(new Student(null, "25", "Aava")); 添加到您的测试中。
猜你喜欢
  • 2021-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-02
  • 2012-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多