/* * This buttons also uses bubble sort to sort all the students in the array * but uses alphabetical sort on the name fields */ private void SortAlphaButtonActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: Student tempStudent; /* the bubble sort: * each iteration of the outer loop is a "pass" through an interval of the array * where the number of passes is one less than the size of the array */ for (int i = 0; i < (numstudents - 1); i++ ) { // the inner loop iterates over elements of the array in the interval, // which decreases in size by 1 each pass for (int j = 0; j < (numstudents - (i+1)); j++) { // for each student in the array, compare the name fields // using the String compareTo method // returns negative number if less and positive number if more if ((students[j].getStudentname().toLowerCase().compareTo (students[j+1].getStudentname().toLowerCase())) > 0) { tempStudent = students[j]; students[j] = students[j+1]; students[j+1] = tempStudent; } } } // end of sorting // display all students in sorted order in a multiline label String outputtext = "Students sorted by name:

"; // for each student in the (now sorted) array, add that student to the list for (int i = 0; i < numstudents; i++) { outputtext = outputtext + "

" + students[i].toString(); } // display the students using the same label as before ResultLabel.setText(outputtext); }