title: Optimal way to sort a txt file in Java
body: <p>I've got a CSV file that I'm processing using the opencsv library.  So I can read in each line.  The particular transformation I need to do requires me to sort that file first before I run through it with the main portion of my java file.</p>
  
  <p>e.g.</p>
  
  <pre><code>5423, blah2, blah
  5323, blah3, blah
  5423, blah4, blah
  5444, blah5, blah
  5423, blah6, blah
  </code></pre>
  
  <p>should become</p>
  
  <pre><code>5323, blah3, blah
  5423, blah2, blah
  5423, blah4, blah
  5423, blah6, blah
  5444, blah5, blah
  </code></pre>
  
  <p>etc..</p>
  
  <p>The reason i need to do this is I'm combining all rows with the same id and outputting them to a new file. </p>
  
  <p>Anything wrong with:</p>
  
  <ol>
  <li><p>Read each line of the csv with the opencsv library</p></li>
  <li><p>Add them to a 2 dimensional array  </p></li>
  <li><p>Run some sort of sorting on this </p></li>
  <li><p>Loop through sorted array and output to file.</p></li>
  </ol>
  
  <p>Any other ideas on this and what is the best way to sort the data?</p>
  
  <p>Bit rusty on my Java.</p>
  
  <p>UPDATE:
  To Clarify on the final output </p>
  
  <p>It would look like:  </p>
  
  <pre><code>5323, blah3, blah
  5423, blah2!!blah4!!blah6, blah
  5444, blah5, blah
  </code></pre>
  
  <p>This is a very simplified version of what I'm doing. It actually is needed for multi option fields in a JBase system.  This is the requested file format.</p>
  
  <p>There are over a 100,000 lines in the original file. </p>
  
  <p>This will be run more than once and the speed it runs is important to me.</p>