I have an ArrayList that holds 2 types of objects in it (objects from class Student and objects from class Teacher).
My question is how can I sort it so that all objects that are from Student to appear first and then all objects that are from class Teacher to appear after them.
For example: Student1, Student2, Student3, Teacher1,Teacher2,Teacher3
Here is my code:
public ArrayList sortList(){
ArrayList<Student> students = new ArrayList<Student>();
ArrayList<Teacher> teachers = new ArrayList<Teacher>();
ArrayList<Person> university = new ArrayList<Person>();
for(Person p : list){
if(p.getClass().getSimpleName().equals("Teacher")){
teachers.add((Teacher)p);
};
if(p.getClass().getSimpleName().equals("Student")){
students.add((Student)p);
}
university.addAll(students);
university.addAll(teachers);
}
return university;
}