3

I've been trying to sort my ArrayList using Comparable interface. I can't seem to get it to work if you have any tips/fixes that would be great!

Here's the company(bedrijf) class which holds an ArrayList of employees(werknemers):

package nl.hva.oop1.bedrijf.models;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Bedrijf {
    private String naam;
    private List<Persoon> medewerkers = new ArrayList<Persoon>();

    public Bedrijf (String naam) {
        this.naam = naam;
    }

    public void printInkomsten() {
        String inkomstenText = "Inkomsten van alle personen: ";

        Collections.sort(medewerkers); //I want to sort the ArrayList here

        for (Persoon p : medewerkers) {
            if (p.berekenInkomsten() != 0)
                inkomstenText += "\n\t" + p.toString() + ", inkomsten: " + p.berekenInkomsten();
            else
                inkomstenText += "\n\t" + p.toString() + ", bedankt voor uw inzet!";
        }

        System.out.println(inkomstenText);
    }

    public int aantalManagers() {
        int managers = 0;

        for(Persoon p : medewerkers) {
            if(p instanceof Manager)
                managers++;
        }

        return managers;
    }

    public void neemInDienst(Persoon p) {
        this.medewerkers.add(p);
    }

    @Override
    public String toString() {
        String bedrijfText = "Bedrijf HvA heeft " + medewerkers.size() + " medewerkers: ";

        for (Persoon p : medewerkers) {
            bedrijfText += "\n\t" + p.toString();
        }

        return bedrijfText + "\n" ;
    }
}

Here is the Abstract Person(persoon) class (has sub-classes):

package nl.hva.oop1.bedrijf.models;

public abstract class Persoon implements Comparable<Persoon> {
    private String naam;

    public Persoon(String naam) {
        this.naam = naam;
    }

    public abstract double berekenInkomsten();

    @Override
    public int compareTo(Persoon p) {
        return this.naam.compareTo(p.naam);
    }

    @Override
    public String toString() {
        return naam;
    }
}

And the Comparable interface:

package nl.hva.oop1.bedrijf.models;

public interface Comparable<E> { 
    public int compareTo(E o); 
}
1
  • No, don't create your own Comparable interface! Commented Dec 14, 2016 at 2:05

2 Answers 2

3

This is your mistake:

And the Comparable interface:

package nl.hva.oop1.bedrijf.models;

public interface Comparable<E> { 
    public int compareTo(E o); 
}

Don't create your own Comparable interface. Instead use the Comparable interface provided by Core Java. That's the only one that the Collections.sort(...) will recognize.

Sign up to request clarification or add additional context in comments.

1 Comment

I removed the Comparable interface I made and it worked!
0

replace

 Collections.sort(medewerkers); //I want to sort the ArrayList here

with

 Collections.sort(medewerkers,your class object that implement Comparator Interface); //I want to sort the ArrayList here

in order to use your own comparator you should use this API

 public static <T> void sort(List<T> list, Comparator<? super T> c)

to assign your comparator explicitly.

and it seemed there are no class that implement Comparator Interface in your code.

Comments

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.