title: Java - Bubble Sort Objects in Array
body: <p><strong>SOLVED</strong> <em>Fixed code at the end of post along with progression</em></p>
  
  <p>I apologize in advance if my question seems unclear or if I leave anything out. I'm new to programming and java, so I may not know how to phrase my questions appropriately yet. I'm going to set up what you should know in order to help me with my question, and then ask it. I'll continue editing this post if I can make things more clear.</p>
  
  <p>I have an array, arrStudents, of (7) Student objects: (firstName, lastName, grade) (String, String, int)</p>
  
  <p>getGrade() returns the grade</p>
  
  <p><strong>I want to create a method that bubble sorts the array and prints</strong>, so for example, this is the output prior to sort:</p>
  
  <pre><code>John Smith 90
  Barack Obama 95
  Al Clark 80
  Sue Taylor 55
  Ann Miller 75
  George Bush 58
  John Miller 65
  </code></pre>
  
  <p>and here is the sorted output</p>
  
  <pre><code>Sue Taylor 55
  George Bush 58
  John Miller 65
  Ann Miller 75
  Al Clark 80
  John Smith 90
  Barack Obama 95
  </code></pre>
  
  <p>I have some code to work from, but it hasn't gotten me anywhere. Here's the example I was given with bubble sort:</p>
  
  <pre><code>import java.util.Scanner;
  
  class array {
  
  public static void main (String[] args) 
  {
  
  Scanner scan = new Scanner(System.in);
  
  int [] a = new int [100];
  int i, n = 0, min, max;
  float  total = 0;
  
  System.out.println ("Enter integers separated by blanks (&lt;Enter&gt; &lt;Ctrl-Z&gt; to end):");
  
  while (scan.hasNext()) {
     a[n] = scan.nextInt();
     n = n + 1;
  }
  
  min = a[0];
  max = a[0];
  for (i = 0; i &lt; n; i++) {
    if (max &lt; a[i]) max = a[i];
    if (min &gt; a[i]) min = a[i];
    total = total + a[i];
  }
  
  System.out.print ("You entered " + n + " numbers: ");
  System.out.print ("Min = " + min + ", Max = " + max + ", Average = " + total/n);
  
  int t, swap = 0;
  do { 
      swap = 0; 
      for (i=0; i&lt;n-1; i++) 
         if (a[i]&gt;a[i+1]) {
             t=a[i]; 
             a[i]=a[i+1]; 
             a[i+1]=t; 
             swap++;
         }
  } while (swap&gt;0);
  
  System.out.print ("\nSorted: ");
  for (i=0; i&lt;n; i++) System.out.print (a[i] + " ");
  System.out.println ();
  
   }
  }
  </code></pre>
  
  <p>I'm unable to use the array of objects with the > operator, so I I tried using <em>arrStudents[i].getGrade()</em>, but I'm not sure if that was correct, I couldn't get it to give me the correct output.</p>
  
  <p>Here's the code I've been playing with:</p>
  
  <pre><code>public static void bubbleSort(Student [] arrStudents) {
  int t, swap = 0;
  
  do {
      swap = 0;
      for (i=0; i&lt;arrStudents.length-1; i++){
      int grade = arrStudents[i].getGrade();
      int gradePlus = arrStudents[i+1].getGrade();
         if (grade&gt;grade+1) {
             t=grade;
             grade=gradePlus;
             gradePlus=t;
             swap++;              
         }
      }
  } while (swap&gt;0); 
  }
  </code></pre>
  
  <p><strong>Edit: Revised Code (Still needs fixing)</strong></p>
  
  <pre><code>public static void bubbleSort(Student [] arrStudents) {
  int swap = 0;
  
  do {
      swap = 0;
      for (i=0; i&lt;arrStudents.length-1; i++){
      int grade = arrStudents[i].getGrade();
      int gradePlus = arrStudents[i+1].getGrade();
         if (grade&gt;gradePlus) {
          Student tmp = arrStudents[i];
          arrStudents[i] = arrStudents[i+1];
          arrStudents[i+1]=tmp;
          swap++;
          System.out.println(tmp);
         }          
      }
  } while (swap&gt;0);
  }
  </code></pre>
  
  <p>Output of revised code (Descending order is fine):</p>
  
  <pre><code>Barack Obama 95
  Barack Obama 95
  Barack Obama 95
  Barack Obama 95
  Barack Obama 95
  John Smith 90
  John Smith 90
  John Smith 90
  John Smith 90
  John Smith 90
  Al Clark 80
  Al Clark 80
  Al Clark 80
  Al Clark 80
  Ann Miller 75
  Ann Miller 75 
  </code></pre>
  
  <p><strong>Fixed Code (with Ascending Output)</strong> - I was just stupid with the print from my previous attempt</p>
  
  <pre><code>public static void bubbleSort(Student [] arrStudents) {
  int swap = 0;    
  do {
      swap = 0;
      for (i=0; i&lt;arrStudents.length-1; i++){
        Student tmp = arrStudents[i];  
        int grade = arrStudents[i].getGrade();
        int gradePlus = arrStudents[i+1].getGrade();
         if (grade&gt;gradePlus) {
          arrStudents[i] = arrStudents[i+1];
          arrStudents[i+1]=tmp;
          swap++;
         }          
      }
  } while (swap&gt;0);    
  System.out.print ("\nSorted: ");
  for (i=0; i&lt;arrStudents.length; i++) 
  System.out.print ("\n" + arrStudents[i]);
  }
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <pre><code>Sorted:
  Sue Taylor 55
  George Bush 58
  John Miller 65
  Ann Miller 75
  Al Clark 80
  John Smith 90
  Barack Obama 95
  </code></pre>
  
  <p>Been stumped for awhile on this one, so any help would be much appreciated!</p>
  
  <p><em>tl;dr Help me fix the closest above bubble sort code</em></p>
  
  <p>Edit: Also aware that there may be better ways to sort, but for this program, I need to use bubble sort.</p>