title: How to sort a linked list of Polynomials by degree?
body: <p>Currently I've written a method that adds two polynomials together. Poly1 and Poly2. The logic of the method is as follows, first it adds all matching degree terms from Poly1 and Poly2, then it adds all non-matching terms from Poly1, and finally adds all non-matching terms from Poly2. But because of this, the terms are out of order.</p>
  
  <pre><code>Polynomial answer = new Polynomial();
  
      for (Node firstPoly = poly; firstPoly != null; firstPoly = firstPoly.next){
          boolean polyAdded = false;
          for (Node secondPoly = p.poly; secondPoly != null; secondPoly = secondPoly.next){
  
              if (firstPoly.term.degree == secondPoly.term.degree){
  
              answer = addToRear(answer, (firstPoly.term.coeff + secondPoly.term.coeff), firstPoly.term.degree, null);
                      if (answer.poly.term.coeff == 0){
                          answer.poly = null;
                      }
                      polyAdded = true;           
              }
  
  
          }
          if (polyAdded == false){
          answer = addToRear(answer, firstPoly.term.coeff, firstPoly.term.degree, null);
          if (answer.poly.term.coeff == 0){
              answer.poly = null;
          }
          }
  
      }
  
      for (Node secondPoly = p.poly; secondPoly != null; secondPoly = secondPoly.next){
          boolean match = false;
          for (Node answerPoly = answer.poly; answerPoly != null; answerPoly = answerPoly.next){
              if (secondPoly.term.degree == answerPoly.term.degree){
                  match = true;
                  break;
              }
  
          }
          if (match == false){
          answer = addToRear(answer, secondPoly.term.coeff, secondPoly.term.degree, null);
          }
      }
  
      return answer;
  
      //alt + shift + r
  }
  </code></pre>
  
  <p>If this code outputs:</p>
  
  <p>8.0x^4 + 4.0x^5 + 2.0x^3 + -1.0x + 12.0</p>
  
  <p>the linked list is represented as such:</p>
  
  <p>(coefficient, degree) // (12, 0) -> (-1, 1) -> (2, 3) -> (4, 5) -> (8, 4)</p>
  
  <p>I want to now sort my answer polynomial by order of degree. The linked list should be represented like this:</p>
  
  <p>(coefficient, degree) // (12, 0) -> (-1, 1) -> (2, 3) -> (8, 4) -> (4, 5)</p>
  
  <p>EDIT: Found the solution on my own. Here is the sorting method I created:</p>
  
  <pre><code>private Polynomial sortByDegree(Polynomial p){
      Node prev = p.poly;
      Node current = p.poly.next;
  
      while (current != null){
          if (current.term.degree &lt; prev.term.degree){
              int temp = current.term.degree;
              current.term.degree = prev.term.degree;
              prev.term.degree = temp;
  
              float temp2 = current.term.coeff;
              current.term.coeff = prev.term.coeff;
              prev.term.coeff = temp2;
  
              prev = p.poly;
              current = p.poly.next;
          }
  
          prev = prev.next;
          current = current.next;
      }
  
      return p;
  }
  </code></pre>
  
  <p>Thanks everyone!</p>