-3

I am hard stuck on a problem I cannot find a good answer to. I've found this one about custom comparators, but it is incomplete:

class YourClass {

    static Comparator<YourClass> getAttribute1Comparator() {
        return new Comparator<YourClass>() {
            // compare using attribute 1
        };
    }

    static Comparator<YourClass> getAttribute2Comparator() {
        return new Comparator<YourClass>() {
            // compare using attribute 2
        };
    }
}

That should work, but I don't know how the comparison part works. Here is my class:

package ZVCVolkel_Logic;

import java.util.Comparator;

public class Vliegtuig implements Comparator<Vliegtuig>{

    private String naam;
    private String type;
    private String status;

    private Hangaar hangaar;

    public Vliegtuig(String naam, String type, String status, Hangaar hangaar){
        this.naam = naam;
        this.type = type;
        this.status = status;
        this.hangaar = hangaar;
    }
}

Now I need a comparator for status and for Hangaar.getName(). Can someone help?

It is not the one, he has only 1 comparator. I can get that working too but not with 2 different ones in 1 class.

MrEmper
  • 225
  • 1
  • 4
  • 18
  • 1
    and the question is? – payloc91 May 11 '18 at 14:42
  • Did you read the docs? Comparison is pretty well-defined. – Dave Newton May 11 '18 at 14:42
  • How can I make a comparator for status and Hangaar.getName() – MrEmper May 11 '18 at 14:43
  • What is your criterion for comparing `Vliegtuig` instances? Describe this **in words** before you even worry about implementing any code. – Code-Apprentice May 11 '18 at 14:43
  • By implementing the comparison based on your requirements. I'm not sure what the problem is-it seems like you haven't read any docs or searched for similar questions. – Dave Newton May 11 '18 at 14:44
  • The name of the status and the name that comes with Hangaar.getName() – MrEmper May 11 '18 at 14:44
  • When you create two comparators, you can compare objects by status **or** by name. Is this what you really want? – Code-Apprentice May 11 '18 at 14:44
  • @DaveNewton This makes me pretty mad because I have been trying for 2 hours now... – MrEmper May 11 '18 at 14:45
  • That is not a specific, that says what fields you use. If you're using two girls the order of comparison is important. Please read the docs, and search for comparing two fields using Java. – Dave Newton May 11 '18 at 14:45
  • 1
    also your class `Vliegtuig` should probably implement `Comparable` rather than `Comparator` – payloc91 May 11 '18 at 14:46
  • @MrEmper Sorry you're having such a tough time. It's pretty easy, and it's unclear what you've actually tried, because you provide no code. – Dave Newton May 11 '18 at 14:47
  • A comparator defines which object comes "before" another in sort order. For `String`, this is defined by typical dictionary ordering (more or less). How do you compare your objects? Do you want to sort them alphabetically by name? How does the status come into it? – Code-Apprentice May 11 '18 at 14:47
  • And Marko Pacak has a good suggestion. Read about Comparable for now and forget Comparator. You can do what you want more easily with Comparable. – Code-Apprentice May 11 '18 at 14:48
  • 1
    @Code-Apprentice I don't think that will solve the OP's problem-OP needs two comparators, not a single Comparable. – Dave Newton May 11 '18 at 14:49
  • His solution is also with Comparator? I'm making this because I have a User I/O which can call for the Array list and then select if he want it sorted by status or by names of the Hangaar. It will then print it to the console in the order chosen. – MrEmper May 11 '18 at 14:50
  • Possible duplicate of [How to use Comparator in Java to sort](https://stackoverflow.com/questions/2839137/how-to-use-comparator-in-java-to-sort) – explv May 11 '18 at 14:57
  • I suggest that you make your problem simpler. First write a `Comparator` that sorts your `Vliegtuig` objects by name. You need to be able to do this before you can even worry about two comparators. And once you figure out the first one, the second one will be easier. If its easier to sort by status first then do that. Either way, you need to get one Comparator working. – Code-Apprentice May 11 '18 at 17:02

2 Answers2

1

The comparator interface has a method compare return an int value to determine the relation ship between two objects.

It will return:

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

static Comparator<Vliegtuig> hangaarNameComparator() {
    return new Comparator<Vliegtuig>(){
              public int compare(Vliegtuig one, Vliegtuig two) {
                 return one.getHangaar().getName().compareTo(two.getHangaar().getName());
              }
           }
}

Here you probably want to take care of NullPointerException if getHangaar() or hangaar.getName() return null.

In java 8 you could do this:

Comparator<Vliegtuig> hangaarNameComparator = Comparator.comparing(Vliegtuig::getHagaar, 
                            Comparator.comparing(Hagaar::getName));
Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51
-1

In the comparator implementation you need to compare 2 objects. You can refer to most of JDK classes for example, for instance java.lang.Integer.

In your case solution will be to use embedded compactors from objects like this:

Comparator<Vliegtuig> nameComparator = new Comparator<>() {
    @Override
    public int compare(Vliegtuig o1, Vliegtuig o2) {
        return o1.getName().compareTo(o2.getName());
    }
}

And you don't need to extend Comparator by the Vliegtuig.

schaffe
  • 399
  • 2
  • 16