سؤال

I have a class Student with three attribute :

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

And implements 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());
}

Method main:

 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());
    }
}

And result : https://lh6.googleusercontent.com/-IIGbZ4uThRk/Ute08Qt6UJI/AAAAAAAAAsg/ahqgAKgMSHc/w325-h219-no/Capture6.PNG But i want null value is sorted of the first position (sort follow name -> age -> class). How can i do that ?

هل كانت مفيدة؟

المحلول

This method will make null values sort to the top of the list and order the list by name, age and finally classstudy. It uses a small helper function because the compare function repeats itself quite frequently.

@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);
    }
}

نصائح أخرى

Do like this

@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;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top