I'm trying to create a program that can sort the contents of an ArrayList alphabetically. Right now I have three classes in my program...
Dog
public class Dog {
private String name;
public Dog(){
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}
DogList (which contains the ArrayList of type Dog)
import java.util.ArrayList;
public class DogList {
private ArrayList<Dog> dogList;
public DogList(){
DogList = new ArrayList<>();
}
public void setSize(int DogSize){
for(int x = 0; x <= DogSize; x++){
DogList.add(new Dog());
}
}
public ArrayList<Dog> getList(){
return dogList;
}
}
and the last class, DogSorter, which is trying to access DogList ArrayList and then trying to sort the contents of that ArrayList in alphabetical order.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class DogSorter {
private DogList list = new DogList();
public void sortDogs{
Collections.sort(list);
}
for(i = 0; i < list.length(); i++) {
System.out.println(list.getList().get(i).getName();
}
}
I apologize for the long post. My question is simply: How do I sort the contents of the ArrayList accurately? I keep getting an error saying that I'm trying to compare the wrong types with my "Collections.sort" line. If anyone can help out, please let me know. Thank you.
edit: To be clear, I'm trying to sort them alphabetically by the string stored in getName() for each object