/Users/20442/NetBeansProjects/Real IA/src/SortStudent.java
  1 
  2 import java.util.ArrayList;
  3 
  4 /*
  5  * To change this license header, choose License Headers in Project Properties.
  6  * To change this template file, choose Tools | Templates
  7  * and open the template in the editor.
  8  */
  9 
 10 /**
 11  *
 12  * @author 20442
 13  */
 14 //package
 15 
 16 class SortStudent {
 17      
 18         
 19      public void sortByLastNameAscending(ArrayList<Student> students) {
 20         for (int i = 0; i < students.size()-1; i++) {
 21             int minIndex = i;      // Assumed index of smallest remaining value.
 22             for (int j = i+1; j < students.size(); j++) {
 23                 if (students.get(j).getLastName().compareTo(students.get(minIndex).getLastName()) < 1) {
 24                     minIndex = j;  // Remember index of new minimum
 25                 }
 26             }
 27             if (minIndex != i) { 
 28             //Exchange current element with smallest remaining.
 29             //But note that this only happens once each outer loop iteration, at the end of the inner loop's looping
 30                 Student temp = students.get(i);
 31                 students.set(i, students.get(minIndex));
 32                 students.set(minIndex, temp);
 33             }
 34         }
 35     }  
 36      
 37     public void sortByLastNameDescending(ArrayList<Student> students) {
 38         for (int i = 0; i < students.size()-1; i++) {
 39             int minIndex = i;      // Assumed index of smallest remaining value.
 40             for (int j = i+1; j < students.size(); j++) {
 41                 if (students.get(j).getLastName().compareTo(students.get(minIndex).getLastName()) > 1) {
 42                     minIndex = j;  // Remember index of new minimum
 43                 }
 44             }
 45             if (minIndex != i) { 
 46             //Exchange current element with smallest remaining.
 47             //But note that this only happens once each outer loop iteration, at the end of the inner loop's looping
 48                 Student temp = students.get(i);
 49                 students.set(i, students.get(minIndex));
 50                 students.set(minIndex, temp);
 51             }
 52         }
 53     }
 54    
 55      
 56      public void sortByFirstNameAscending(ArrayList<Student> students) {
 57         for (int i = 0; i < students.size()-1; i++) {
 58             int minIndex = i;      // Assumed index of smallest remaining value.
 59             for (int j = i+1; j < students.size(); j++) {
 60                 if (students.get(j).getFirstName().compareTo(students.get(minIndex).getFirstName()) < 1) {
 61                     minIndex = j;  // Remember index of new minimum
 62                 }
 63             }
 64             if (minIndex != i) { 
 65                             //Exchange current element with smallest remaining.
 66                         //But note that this only happens once each outer loop iteration, at the end of the inner loop's looping
 67                 Student temp = students.get(i);
 68                 students.set(i, students.get(minIndex));
 69                 students.set(minIndex, temp);
 70             }
 71         }
 72     }
 73      
 74      public void sortByFirstNameDescending(ArrayList<Student> students) {
 75         for (int i = 0; i < students.size()-1; i++) {
 76             int minIndex = i;      // Assumed index of smallest remaining value.
 77             for (int j = i+1; j < students.size(); j++) {
 78                 if (students.get(j).getFirstName().compareTo(students.get(minIndex).getFirstName()) > 1) {
 79                     minIndex = j;  // Remember index of new minimum
 80                 }
 81             }
 82             if (minIndex != i) { 
 83                             //Exchange current element with smallest remaining.
 84                         //But note that this only happens once each outer loop iteration, at the end of the inner loop's looping
 85                 Student temp = students.get(i);
 86                 students.set(i, students.get(minIndex));
 87                 students.set(minIndex, temp);
 88             }
 89         }
 90     }    
 91      
 92     public void sortByGradeAscending(ArrayList<Student> students) {
 93         int n = students.size();
 94         boolean sorted = false;
 95         while (!sorted) {
 96             n--; //It is the n which will result in one less comparison happening each outer pass;
 97             //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
 98             sorted = true;
 99             for (int i = 0; i < n; i++) {
100                
101                 if(Integer.parseInt(students.get(i).getGrade()) > Integer.parseInt(students.get(i+1).getGrade())) {
102                     Student temp = students.get(i);
103                     students.set( i, students.get(i + 1));
104                     students.set((i+1), temp);
105                     sorted = false; //as in the second bubble sort, if swapping happens we'll want to continue, and so
106                     //with sorted re-set to false again, the while loop continues
107                 }
108             }
109         }
110     }
111     
112     public void sortByGradeDescending(ArrayList<Student> students) {
113         int n = students.size();
114         boolean sorted = false;
115         while (!sorted) {
116             n--; //It is the n which will result in one less comparison happening each outer pass;
117             //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
118             sorted = true;
119             for (int i = 0; i < n; i++) {
120                
121                 if(Integer.parseInt(students.get(i).getGrade()) < Integer.parseInt(students.get(i+1).getGrade())) {
122                     Student temp = students.get(i);
123                     students.set( i, students.get(i + 1));
124                     students.set((i+1), temp);
125                     sorted = false; //as in the second bubble sort, if swapping happens we'll want to continue, and so
126                     //with sorted re-set to false again, the while loop continues
127                 }
128             }
129         }
130     }
131     
132     public void sortByYearAscending(ArrayList<Student> students) {
133         int n = students.size();
134         boolean sorted = false;
135         while (!sorted) {
136             n--; //It is the n which will result in one less comparison happening each outer pass;
137             //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
138             sorted = true;
139             for (int i = 0; i < n; i++) {
140                
141                 if(students.get(i).getYear() > students.get(i+1).getYear() ) {
142                     Student temp = students.get(i);
143                     students.set( i, students.get(i + 1));
144                     students.set((i+1), temp);
145                     sorted = false; //as in the second bubble sort, if swapping happens we'll want to continue, and so
146                     //with sorted re-set to false again, the while loop continues
147                 }
148             }
149         }
150     }
151     public void sortByYearDescending(ArrayList<Student> students) {
152         int n = students.size();
153         boolean sorted = false;
154         while (!sorted) {
155             n--; //It is the n which will result in one less comparison happening each outer pass;
156             //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
157             sorted = true;
158             for (int i = 0; i < n; i++) {
159                
160                 if(students.get(i).getYear() < students.get(i+1).getYear() ) {
161                     Student temp = students.get(i);
162                     students.set( i, students.get(i + 1));
163                     students.set((i+1), temp);
164                     sorted = false; //as in the second bubble sort, if swapping happens we'll want to continue, and so
165                     //with sorted re-set to false again, the while loop continues
166                 }
167             }
168         }
169     }
170     
171      
172     
173     
174     
175 }
176