/Volumes/GoogleDrive/My Drive/IB CS Resources/Final CS IA/20058_Computer_Science_IA/src/coreGUI/SortAndSearchClass.java
  1 package coreGUI;
  2 
  3 import java.util.ArrayList;
  4 import coreGUI.SetOfQs;
  5 
  6 /*
  7  * To change this license header, choose License Headers in Project Properties.
  8  * To change this template file, choose Tools | Templates
  9  * and open the template in the editor.
 10  */
 11 
 12 /**
 13  *
 14  * @author 20058
 15  */
 16 public class SortAndSearchClass {
 17     
 18     //Searching Method (Sequential Search)
 19     public int sequentialSearch(ArrayList<SetOfQs> searchSetOfQs, String searchFieldText){
 20     for(int i = 0; i < searchSetOfQs.size(); i++){
 21         if(searchSetOfQs.get(i).getSetOfQsName().equalsIgnoreCase(searchFieldText)){
 22             return i;
 23         }
 24     }
 25     return -1;
 26 }
 27    
 28     
 29     //Sorting Methods (Selection Sort)
 30     public void sortByNameDescending(ArrayList<SetOfQs> setOfQsArr) {
 31     for (int i = 0; i < setOfQsArr.size()-1; i++) {
 32         int minIndex = i;      // Assumed index of smallest remaining value.
 33         for (int j = i+1; j < setOfQsArr.size(); j++) {
 34             if (setOfQsArr.get(j).getSetOfQsName().compareTo(setOfQsArr.get(minIndex).getSetOfQsName()) < 1)  {
 35                 minIndex = j;  // Remember index of new minimum
 36             }
 37         }
 38         if (minIndex != i) { 
 39             //Exchange current element with smallest remaining.
 40             //But note that this only happens once each outer loop iteration, at the end of the inner loop's looping
 41             SetOfQs temp = setOfQsArr.get(i);
 42             setOfQsArr.set(i, setOfQsArr.get(minIndex));
 43             setOfQsArr.set(minIndex, temp);
 44         }
 45     }
 46 }
 47     
 48     public void sortByNameAscending(ArrayList<SetOfQs> setOfQsArr) {
 49     for (int i = 0; i < setOfQsArr.size()-1; i++) {
 50         int minIndex = i;      // Assumed index of smallest remaining value.
 51         for (int j = i+1; j < setOfQsArr.size(); j++) {
 52             if (setOfQsArr.get(j).getSetOfQsName().compareTo(setOfQsArr.get(minIndex).getSetOfQsName()) > 1)  {
 53                 minIndex = j;  // Remember index of new minimum
 54             }
 55         }
 56         if (minIndex != i) { 
 57             //Exchange current element with smallest remaining.
 58             //But note that this only happens once each outer loop iteration, at the end of the inner loop's looping
 59             SetOfQs temp = setOfQsArr.get(i);
 60             setOfQsArr.set(i, setOfQsArr.get(minIndex));
 61             setOfQsArr.set(minIndex, temp);
 62         }
 63     }
 64 }
 65     
 66     public void sortByAmountAscending(ArrayList<SetOfQs> setOfQsArr) {
 67     for (int i = 0; i < setOfQsArr.size()-1; i++) {
 68         int minIndex = i;      // Assumed index of smallest remaining value.
 69         for (int j = i+1; j < setOfQsArr.size(); j++) {
 70             if (setOfQsArr.get(j).getNumberOfQ() < setOfQsArr.get(minIndex).getNumberOfQ())  {
 71                 minIndex = j;  // Remember index of new minimum
 72             }
 73         }
 74         if (minIndex != i) { 
 75             //Exchange current element with smallest remaining.
 76             //But note that this only happens once each outer loop iteration, at the end of the inner loop's looping
 77             SetOfQs temp = setOfQsArr.get(i);
 78             setOfQsArr.set(i, setOfQsArr.get(minIndex));
 79             setOfQsArr.set(minIndex, temp);
 80         }
 81     }
 82 }
 83     
 84     public void sortByAmountDescending(ArrayList<SetOfQs> setOfQsArr) {
 85     for (int i = 0; i < setOfQsArr.size()-1; i++) {
 86         int minIndex = i;      // Assumed index of smallest remaining value.
 87         for (int j = i+1; j < setOfQsArr.size(); j++) {
 88             if (setOfQsArr.get(j).getNumberOfQ() > setOfQsArr.get(minIndex).getNumberOfQ())  {
 89                 minIndex = j;  // Remember index of new minimum
 90             }
 91         }
 92         if (minIndex != i) { 
 93             //Exchange current element with smallest remaining.
 94             //But note that this only happens once each outer loop iteration, at the end of the inner loop's looping
 95             SetOfQs temp = setOfQsArr.get(i);
 96             setOfQsArr.set(i, setOfQsArr.get(minIndex));
 97             setOfQsArr.set(minIndex, temp);
 98         }
 99     }
100 }
101    
102      
103 }
104 
105     
106 
107