2

I'm aware that it's a trivial matter, still I can't figure out how to implement the sort() function on my Vector. I'll paste the example I'm currently working on: it's a Zoo (that is a Vector of animals)

Class Zoo:

import java.util.Vector;
public class Zoo {

//attributes
private Vector<Animal> animals;

//builder
public Zoo() {
    animals = new Vector<Animal>();
}

//function that adds an animal to my Zoo
public void addAnimal(Animal a) {
    animal.add(a);
}

//function that removes an animal from my Zoo
public void removeAnimal(Animal a) {
    animals.remove(animals.indexOf(a));
}

//function that prints a list of animals currently in my Zoo
public void view() {
    System.out.println(animals);
}
}

Class Animal:

public class Animal{

//attributes (name and species)
private String name;
private String species;

//setters and getters
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getSpecies() {
    return species;
}
public void setSpecies(String species) {
    this.species = species;
}

//builder
public Animal(String name, String species) { 
    this.name = name;
    this.species = species;
}

//override of toString from Object class
public String toString() {
    return "The animal " + getName() + " belongs to the family of " + getSpecies() + "\n";
}

//override of equals from Object Class
public boolean equals(Object other) {
    return other instanceof Animal
                && getName().equals(((Animal)other).getName())
                    &&getSpecies().equals(((Animal)other).getSpecies());
}
}

My question is: what do I need for the sort() method to work? I've tried:

-Making the Animal class implement Comparable (and, therefore, writing my compareTo(Animal other) function)

-Making the Animal class implement Comparator (and, therefore, writing my own comprare(Animal a, Animal b) function)

the error I keep getting is:

the method sort(Comparator<?Super Animal> in the type Vector<Animal> 
is not applicable for the arguments.

Would I get anything different if I were using ArrayList instead of Vector? (I'm using vector since I've been taught to use it in school, I'm aware that it's not exactly the most fresh class out there)

Michael Vigato
  • 326
  • 2
  • 11
  • `Would I get anything different if I were using ArrayList instead of Vector` Sense of mind, a java class that might not be older than you, lots of things! And for those, there's `Collections#sort` – Rogue Aug 31 '17 at 16:23
  • 1
    Implement `Comparable` and read the interface's documentation to make sure you obey all the rules. You do not "implement" `Comparator`, this one is independent from your Class. Also, Vector is deprecated, you should use an ArrayList instead. – Guillaume F. Aug 31 '17 at 16:35
  • 2
    You can use `sort()` with no argument if the elements of the collection are `Comparable` **or** use a specified `Comparator` with `sort(Comparator)` –  Aug 31 '17 at 16:54
  • 1
    See also: https://stackoverflow.com/questions/2266827/when-to-use-comparable-and-comparator – Sean Van Gorder Aug 31 '17 at 17:49

1 Answers1

1

Thank you for the suggetions, I've done this. I'll write down how just in case someone has my same problem.

First of all, I've used ArrayList instead of Vector. I've implemented Comparable in my Animal class. I wrote my compareTo(Animal other) method. I called Collections.sort(animals)

Michael Vigato
  • 326
  • 2
  • 11