0

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 ?

3
  • So if a name is null you want it to be first? Commented Jan 16, 2014 at 10:39
  • 4
    Your compareTo need to be consistent. a.compareTo(b) == -b.compareTo(a) but if the name is null in both cases you always return -100. You should compare each field at a time, not try to combine them. Commented Jan 16, 2014 at 10:44
  • 1
    By the way, note that there is not difference between returning -1, -50 or -100 in the compareTo contract docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html Commented Jan 16, 2014 at 10:51

2 Answers 2

1

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);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. But i want null value is highest priority. Example : Name : Hong, Age : 26, Class : Uava and Name : Tung, Age : null, Class : Aava. So Name : Tung, Age : null, Class : Aava in front of Name : Hong, Age : 26, Class : Uava ???
0

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

1 Comment

Try adding lists.add(new Student(null, "25", "Aava")); to your test.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.