Using a Bubble Sort Algorithm, Sort an arrayList of objects alphabetically

Viewed 63

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.

1 Answers

Please try this one:

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

public class BubbleSortAlph {

    public static void alphabetOrder(List<Student> users) {

        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()) > 0) {
                    swap(users, j, j + 1);
                    exchangeMade = true;
                }
            }
        }
    }


    public static void swap(List<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

    public static void main(String[] args) {
        List<Student> users = new ArrayList<>();

        Student student1 = new Student("1234");
        Student student2 = new Student("234");
        Student student3 = new Student("_1234");
        Student student4 = new Student("a");
        Student student5 = new Student("b");
        Student student6 = new Student("ab");

        users.add(student5);
        users.add(student1);
        users.add(student3);
        users.add(student4);
        users.add(student6);
        users.add(student2);

        alphabetOrder(users);

        for (Student student : users) {
            System.out.println(student.getName());
        }
    }
}

You can verify the result here: online sort tool

Related