title: Alphabetically sort LinkedList without using Collections by first and last name in Java
body: <p>I am trying to write code which creates a linked list of names in alphabetical order from a file. </p>
  
  <p>The names must be ordered by last name, then first name. The second layer of sorting is where I am having trouble.</p>
  
  <p>I'm including my code so far, and right now I'm just trying to rewrite <code>sortList</code> so that it goes through the linked list and adds a node after comparing. I wrote a <code>sortList</code> previously using collections and it ran perfectly, now I am trying to do it without collections.  </p>
  
  <p>My suspicion is that this is easier than I'm making it, and I'm just too close to the code at the moment. Below is the <code>sortList</code> and <code>compareTo</code> method... the project constructor just contains <code>firstName</code> and <code>lastName</code>.</p>
  
  <pre><code>public int compareTo(linkedListProject arg0) {
      if (this.lastName.equals(arg0.getLastName()))
          return this.firstName.compareTo(arg0.getFirstName());
      else
          return this.lastName.compareTo(arg0.getLastName());
  }
  </code></pre>
  
  <p>EDIT (Changed sortList to more accurately utilize compareTo)</p>
  
  <pre><code>public static void sortList(linkedListProject a)
  {
      Node temp = new Node(a);
      if (head == null)
              {head = temp;}
      else {
          Node cur = head;
          while (cur.next == null &amp;&amp; cur.data.compareTo(temp) &gt; 0)
          { cur = cur.next;}
      if (cur.next.data.compareTo(temp) &lt;= 0)
          temp = cur; }
  }
  </code></pre>
  
  <p>EDIT once again... Basically I realized Im gonna have to check nodes and insert Nodes as I go, so here is my complete code. sortList has been renamed as addToList. I am still getting nullpointer exceptions</p>
  
  <pre><code>import java.util.*;
  import java.io.*;
  /**
   * @author Michael Dangelo
   * linkedListProject class, reads from file a provided classRoster then sorts them alphebetically by last name into the linkedList
   *
   */
  public class linkedListProject {
  private String firstName;
  private String lastName;
  private static LinkedList&lt;linkedListProject&gt; classRoster  = new LinkedList&lt;linkedListProject&gt;();
  
  private static class Node
  {
      linkedListProject data;
      private Node next = null;
  
      private Node(linkedListProject data, Node next)
      {
          this.data = data;
          this.next = next;
      }
  
      private Node(linkedListProject data)
      {
          this.data = data;
      }
  
      public linkedListProject getData()
      {
          return data;
  
      }
  
  }
  
  private static Node head;
  
  /**
   * Default Constructor
   */
  public linkedListProject()
  {
      head = null;
  }
  
  /**
   * Constructor
   * @param firstName First name of student
   * @param lastName  Last Name of Student
   */
  public linkedListProject(String firstName, String lastName)
  {
      this.firstName = firstName;
      this.lastName = lastName;
  }
  public void addToStart(linkedListProject data)
  {
      head = new Node(data, head);
  }
  /**
   * Getter for firstName
   * @return firstName
   */
  public String getFirstName() 
  {
      return firstName;
  }
  
  /**
   * Setter for firstName
   * @param firstName
   */
  public void setFirstName(String firstName) 
  {
      this.firstName = firstName;
  }
  /**
   * Getter for lastName
   * @return lastName
   */
  public String getLastName() 
  {
      return lastName;
  }
  
  /**
   * Setter for Last Name
   * @param lastName
   */
  public void setLastName(String lastName) 
  {
      this.lastName = lastName;
  }
  
  /**
   * toString method used for testing
   */
  
  @Override
  public String toString() 
  {
      return "linkedListProject [firstName=" + firstName + ", lastName="
              + lastName + "]";
  }
  
  
  
  /**
   * compareTo method used in sorting
   */
  
  
  /**
   * Prints out list to console
   */
  public void printList() 
  {
      int count = 0;
      System.out.println("First Name:     " + "Last Name: ");
      while (count &lt; classRoster.size())
      {
          linkedListProject a = classRoster.get(count);
          System.out.println(a.getFirstName() + "     \t" + a.getLastName());
          count++;
      }
  
  }
  /**
   * 
   * @param a linked list ran in 
   */
  public static void addToList(linkedListProject a)
  {
      Node temp = head;
      Node nextNode = null;
      while (temp != null &amp;&amp; nextNode.data.compareTo(a) &gt;=0)
      {
          nextNode = temp;
          temp = temp.next;
      }
      if (temp == null)
      {
          Node cur2 = new Node(a);
          cur2.next = head;
          head = cur2;    
          classRoster.add(cur2.getData());
      }
      else {
  
          Node cur2 = new Node(a);
          cur2.next = temp;
          if (nextNode == null) {
              nextNode.next = cur2;
              classRoster.add(cur2.getData());
          }
      }
  }
  /**
   * Reads in students names into object then adds and sorts list based on last name &amp; firstName
   * @param inputFileName The File Name
   * @throws java.io.IOException
   */
  public void read(String inputFileName) throws java.io.IOException
  {
  Scanner infile = new Scanner(new FileReader(inputFileName));
  while(infile.hasNext())
  {
  String firstName = infile.next();
  String lastName = infile.next();
  
  linkedListProject intoList = new linkedListProject(firstName, lastName);
  linkedListProject.addToList(intoList);
  
  }
  infile.close();
  }
  
  /**
   * Driver
   * @param args
   * @throws IOException
   */
  public static void main(String[] args) throws IOException 
  {
      linkedListProject test = new linkedListProject();
      test.read("179ClassList.txt");
      test.printList();
  }
  
  public int compareTo(linkedListProject arg0)
  {
      if (this.lastName.equals(arg0.getLastName()))
          return this.firstName.compareTo(arg0.getFirstName());
      else
          return this.lastName.compareTo(arg0.getLastName());
  }
  
  
  }
  </code></pre>