1

I have a list of Student objects, each of them has 3 properties. Color, Size and their names. I want to sort them in ascending order based on their color first, followed by descending order of size and finally, in ascending order of their names. For example, if input is (first line name, second line color and size):

Maria Jose
branco P
Mangojata Mancuda
vermelho P
Cezar Torres Mo
branco P
Baka Lhau
vermelho P
JuJu Mentina
branco M
Amaro Dinha
vermelho P
Adabi Finho
branco G
Severina Rigudinha
branco G
Carlos Chade Losna
vermelho P

I want output to be as:

branco P Cezar Torres Mo
branco P Maria Jose
branco M JuJu Mentina
branco G Adabi Finho
branco G Severina Rigudinha
vermelho P Amaro Dinha
vermelho P Baka Lhau
vermelho P Carlos Chade Losna
vermelho P Mangojata Mancuda

However, after overriding my compareTo() method so that the list is sorted based on what I want, I'm getting output as:

branco P Maria Jose
branco P Cezar Torres Mo
branco M JuJu Mentina
branco G Adabi Finho
branco G Severina Rigudinha
vermelho P Mangojata Mancuda
vermelho P Baka Lhau
vermelho P Amaro Dinha
vermelho P Carlos Chade Losna

As it can be seen, the color and size are sorted correctly however, I'm having trouble with sorting the names. I've tried multiple methods but none works. I can't understand why. Below is my compareTo() method:

public int compareTo(Student student) {
    int value1 = color.compareTo(student.color);
    if (value1 < 0) {
        int value2 = size.compareTo(student.size);
        if (value2 < 0) {
            int value3 = name.compareTo(student.name);
            if (value3 < 0) {
                return value3;
            }
        }
    }
    return value1;
}

Could someone help me out?

Sammie
  • 17
  • 5
  • just add the rest of the code needed. not only for value1 < 0, but also for value1 == 0 and value1 > 0 – Stultuske Apr 26 '22 at 07:26
  • @Stultuske could you elaborate a bit please, what is the rest of the code needed? This is my first time using compareTo() and I thought we only return something when all of the conditions are fulfilled? – Sammie Apr 26 '22 at 07:33
  • "we only return something when all of the conditions are fulfilled" of course not. Either you return something, or that code won't compile. But so far, you only implemented it for one specific case, you'll need to implement the other cases as well – Stultuske Apr 26 '22 at 07:35

3 Answers3

1

You should go on with comparing the other fields only if the preceding ones are equal, meaning your compareTo result is 0, not greater than 0.

You can take a look here.

Vladimir Stanciu
  • 1,468
  • 1
  • 7
  • 24
  • "You should go on with comparing the other fields only if the preceding ones are equal" It took me a while to get what you mean, but now I understand clearly and after editing my code accordingly, it's running perfectly. Thank you. – Sammie Apr 26 '22 at 07:49
0

I am assuming that you want to sort the data based on ascending order of color, if color is equal then you want to sort in descending order of size, if size is also equal, then sort on ascending order of name. Moreover your java class has all these properties as string type.

public int compareTo(Student student) {
    // If color are not equal, sort ascending on color
    if(!this.color.equals(student.color)){
        return this.color.compareTo(student.color);
    }
    // If color are equal, and size is not equal sort descending on size
    if(!this.size.equals(student.size)){
        return student.size.compareTo(this.size);
    }
    // If color and size are equal, sort ascending on name
    return this.name.compareTo(student.name)      
}
Ujwal Kumar
  • 176
  • 6
0

The Comparator interface now offers methods to more conveniently solve your problem.

Add a static variable to your class.

static Comparator < Student > comparator =
    Comparator
    .comparing( Student :: getColor ) 
    .thenComparing( Student :: getSize ).reversed()
    .thenComparing( Student :: getName() 
;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154