It sorts names in ArrayList, but how exactly does it happen?

public void sortStudentsAlphabetic() {

        int i, j;
        Student temp;

        Student[] array = new Student[studentsList.size()];

        for (int k = 0; k < array.length; k++) {
            array[k] = studentsList.get(k);
        }

        for (i = 0; i < array.length - 1; i++) {
            for (j = i + 1; j < array.length; j++) {
                if (array[i].getName().compareToIgnoreCase(array[j].getName()) > 0) {
                     temp = array[i];
                     array[i] = array[j];
                     array[j] = temp; 
                }
            }
        }

        studentsList.clear();

        for(Student student : array) {
            studentsList.add(student);
        }

    }

It's called a "bubble sort" - just Google for details.

Apart from tjhat it just copies a List of students into an array so the sort will be fast, sorts the array, then copies the sorted reults back into the list. That part of the code should work, but it's not as good as it could be.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.