Basically I have a student class that reads a textile which takes in the name, age and student number. I have all this stored in an arraylist with the class.
//method for ordering student accounts by last name (bubble sort) alphabetically
public static void alphabetOrder(ArrayList<Student> users)throws IOException{
int k = 0;
boolean exchangeMade = true;
while ((k < users.size() - 1) && exchangeMade) {
exchangeMade = false; // no exchange has been made yet
k++; // increment the index
for (int j = 0; j < users.size() - k; j++) {
if(users.get(j).getName().compareTo(users.get(j+1).getName()){
swap(users, j, j+1);
exchangeMade = true;
}
}
}
public static void swap(ArrayList<Student> users, int x, int y) {
Student temp = users.get(x); // set temp value to element at index x
users.set(x, users.get(y)); // set value at index x to the value at index y
users.set(y, temp); // set value at index y to the temp value
} // end of swap method
The getName(). method gets the last name of the student. I'm unsure on how to sort this. Would it be better to use name.charAt(i) and then do some ordering by that.