title: Sort data in a text file in alphabetical order - Java
body: <p>I want the output to list the userNameGenerator in alphabetical order. I converted the HashSet into an ArrayList because sets cannot be sorted, so I changed it to an ArrayList to sort out the list in alphabetical order. I used Collections.sort to sort the list of strings in alphabetical order, however, the output is still 
  the same.</p>
  
  <p>Code I used for alphabetical order:</p>
  
  <pre><code>List sortedList = new ArrayList(personFile);
  Collections.sort(sortedList); 
  System.out.println();
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Dompteuse -&gt; Imran Sullivan,
  Deservedness -&gt; Eadie Jefferson,
  Ostracize -&gt; Eadie Jefferson,
  Abattoir -&gt; Angel Whitehouse,
  Choreography -&gt; Imran Sullivan,Taylor Vargas,Priya Oliver,
  Goodfella -&gt; Khalil Person,Eadie Jefferson,
  Frolicwas -&gt; Taylor Vargas,
  DarknianIdeal -&gt; Sophia Jeffery,Clyde Calderon,Taylor Vargas,Liyah Navarro,
  Cremaster -&gt; Aryan Hess,
  Reliable -&gt; Imran Sullivan,Aryan Hess,Angel Whitehouse,Priya Oliver,
  Hootenany -&gt; Clyde Calderon,
  Acrimony -&gt; Taylor Vargas,
  </code></pre>
  
  <p>Full Code:</p>
  
  <pre><code>import java.util.*;
  import java.io.*;
  
  public class Codes {
      public static void main(String[] args) { 
          List&lt;Codes2&gt; personFile = new ArrayList&lt;&gt;();
  
          try {   
              BufferedReader br = new BufferedReader(new FileReader("person-data.txt"));
              String fileRead = br.readLine();
              while (fileRead != null) {
                  String[] personData = fileRead.split(":");                
                  String personName = personData[0];
                  String userNameGenerator = personData[1];                
                  Codes2 personObj = new Codes2(personName, userNameGenerator);               
                  personFile.add(personObj);
                  fileRead = br.readLine();
              }
              br.close();
          }
  
          catch (FileNotFoundException ex) {            
              System.out.println("File not found!");
          } 
  
          catch (IOException ex) {             
              System.out.println("An error has occured: " + ex.getMessage());
          }
  
          Set&lt;String&gt; newStrSet = new HashSet&lt;&gt;();
          for(int i = 0; i &lt; personFile.size(); i++){
              String[] regionSplit = personFile.get(i).getUserNameGenerator().split(", ");
              for(int j = 0; j &lt; regionSplit.length; j++){
                  newStrSet.add(regionSplit[j]);
              }
          }
  
          for (String s: newStrSet) {
              System.out.printf("%s -&gt; ", s);
              for (Codes2 p: personFile) {
                  if (p.getUserNameGenerator().contains(s)) {
                      System.out.printf("%s, ", p.getPersonName());
                  }
              } 
              List sortedList = new ArrayList(personFile);
              Collections.sort(sortedList); 
              System.out.println();
          }       
      }
  }
  </code></pre>
  
  <p>Person Class:</p>
  
  <pre><code>public class Codes2 implements Comparable&lt;Codes2&gt; {
      private String personName;
      private String userNameGenerator;
  
      public Codes2(String personName, String userNameGenerator) {
          this.personName = personName;
          this.userNameGenerator = userNameGenerator;
      }
  
      public String getPersonName() {
          return personName;
      }
  
      public String getUserNameGenerator() {
          return userNameGenerator;
      }
  
      @Override
      public int compareTo(Codes2 o) {
          return getUserNameGenerator().compareTo(o.getUserNameGenerator());
      }
  
      public int compare(Object lOCR1, Object lOCR2) {                
          return ((Codes2)lOCR1).userNameGenerator                        
                  .compareTo(((Codes2)lOCR2).userNameGenerator);            
      }
  }
  </code></pre>
  
  <p>Reference: <a href="https://stackoverflow.com/questions/11176227/simple-way-to-sort-strings-in-the-case-sensitive-alphabetical-order">Simple way to sort strings in the (case sensitive) alphabetical order</a></p>