/Users/johnr/Desktop/IA_14_-_Stage_P_Upload_all_2021-04-08/IBCSIA21296 Isaac/src/fitnesstracker/SearchandSort.java
 1 /*
 2  * To change this license header, choose License Headers in Project Properties.
 3  * To change this template file, choose Tools | Templates
 4  * and open the template in the editor.
 5  */
 6 package fitnesstracker;
 7 
 8 import java.util.LinkedList;
 9 
10 /**
11  *
12  * @author 21296
13  */
14 public class SearchandSort {
15     public SearchandSort(){
16         
17     }
18     //LinkedList<Workout> scheduleQueue = new LinkedList<Workout>();
19     public void sortByWeek(LinkedList<Workout> scheduleQueue) {
20      boolean sorted = false;
21      while (!sorted) {
22           sorted = true; //this is just an assumption, which every time except the last, will
23                          //actually not be the case so the last time through the while loop,
24                          //when we don't get into the swap at least once, the while will no longer run
25           for (int i = 0; i< scheduleQueue.size() - 1; i++) {
26                //(So this bubble sort inefficiently checks all neighbors each pass)
27                if (scheduleQueue.get(i).getWeekNumber() > scheduleQueue.get(i + 1).getWeekNumber()) {
28                     //Swap
29                     Workout temp = scheduleQueue.get(i);  
30                     scheduleQueue.set(i, scheduleQueue.get(i + 1));
31                     scheduleQueue.set(i + 1, temp);
32                     sorted = false;  //sorted goes back to false so we still have to continue sorting
33                }
34           }
35      }
36 }
37 }
38