title: Using a method to sort a Singly Linked List in Java
body: <p>Implementing a Linked List, store up to 10 names, ordered in first in First Out. Then implement two methods, one of which to sort it alphabetically by last names. This is where I am having some trouble. Here is what I tried:</p>
  
  <ol>
  <li><p>Recursion. The method calls two nodes, compare them, swap if needed and then calls itself. Doesn't work with odd number of names and tends to be full bugs.</p></li>
  <li><p>Collection but I don't know enough about it to use it effectively. </p></li>
  <li><p>Sorting algorithms (ex. bubble sort): I can go though the list but have a hard time getting the nodes to swap.</p></li>
  </ol>
  
  <p>My question is: What is the easiest way to do this? </p>
  
  <pre><code>public class List
  {
      public class Link
      {
          public String firstName;
          public String middleName;
          public String lastName;
          public Link next = null;
  
      Link(String f, String m, String l)
      {
          firstName = f;
          middleName = m; 
          lastName = l;
      }
  }
  
  private Link first_;
  private Link last_;
  
  List()
  {
      first_ = null;
      last_ = null;
  }
  
  public boolean isEmpty()
  {
      return first_ == null;
  }
  
  public void insertFront(String f, String m, String l)
  {
      Link name = new Link(f, m, l);
      if (first_ == null)
      {
          first_ = name;
          last_ = name;
      }
      else
      {
          last_.next = name;
          last_ = last_.next;
      }
  }
  
  public String removeFront()
  {
      String f = first_.firstName;
      String m = first_.middleName;
      String l = first_.lastName;
      first_ = first_.next;
      return f + " " + m + " " + l;
  }
  
  public String findMiddle(String f, String l)
  {
      Link current = first_;
      while (current != null &amp;&amp; current.firstName.compareTo(f) != 0 &amp;&amp; current.lastName.compareTo(l) != 0)
      {
          current = current.next;
      }
      if (current == null)
      {
          return "Not in list";
      }
      return "That person's middle name is " + current.middleName;
  }
  }
  
  public class NamesOfFriends
  {
      public static void main(String[] args)
      {
          List listOfnames = new List();
          Scanner in = new Scanner(System.in);
  
      for(int i = 0; i &lt; 3; i++)
      {
          if(i == 0)
          {
              System.out.println("Please enter the first, middle and last name?");
              listOfnames.insertFront(in.next(), in.next(),in.next());
          }
          else
          {
              System.out.println("Please enter the next first, middle and last name");
              listOfnames.insertFront(in.next(), in.next(),in.next());
          }
      }
  
      System.out.println("To find the middle name, please enter the first and last name of the person.");
      System.out.println(listOfnames.findMiddle(in.next(),in.next()));
      }
  }
  </code></pre>
  
  <h1>Edit</h1>
  
  <p>After working on it a bit, I figured out how to go about sorting it. For that purpose, I am trying to implement a remove method that can remove a node anywhere on the list. While it does compile, it doesn't do anything when I run the program. </p>
  
  <pre><code>public Link remove(String lastName)
  {
      Link current_ = first_;
      Link prior_ = null;
      Link temp_ = null;
      while (current_ != null &amp;&amp; current_.lastName.compareTo(lastName) != 0)
      {
          prior_ = current_;
          current_ = current_.next;
      }
      if (current_ != null)
      {
          if (current_ == last_)
          {
              temp_ = last_;
              last_ = prior_;
          }
          else if (prior_ == null)
          {
              temp_ = first_;
              first_ = first_.next;
          }
      }
      else
      {
          temp_ = current_;
          prior_.next = current_.next;
      }
      return temp_;
  }
  </code></pre>