Output file in a certain way via Java <p>Having a little bit of an issue. I have successfully output a file based on the timestamp order, however, theres another condition i am trying to add also to alphabetically order if the time stamp is the same.</p> <p>for example:</p> <p>[TIMESTAMP = 12:30][EVENT=B]</p> <p>[TIMESTAMP = 12:30][EVENT=U]</p> <p>[TIMESTAMP = 12:30][EVENT=A]</p> <p>and i want it to output</p> <p>[TIMESTAMP = 12:30][EVENT=A]</p> <p>[TIMESTAMP = 12:30][EVENT=B]</p> <p>[TIMESTAMP = 12:30][EVENT=U]</p> <p>my current code at the moment stands:</p> <pre><code>package Organiser; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Organiser { public static void main(String[] args) throws FileNotFoundException { ArrayList&lt;String&gt; lines = new ArrayList&lt;&gt;(); String directory = "C:\\Users\\xxx\\Desktop\\Files\\ex1"; Scanner fileIn = new Scanner(new File(directory + ".txt")); PrintWriter out = new PrintWriter(directory + "_ordered.txt"); while (fileIn.hasNextLine() == true) { lines.add(fileIn.nextLine()); Collections.sort(lines); System.out.println("Reading..."); } for (String output : lines) { out.println(output + "\n"); } out.close(); System.out.println("Complete - See " + directory + "_ordered.txt"); } } </code></pre> <p>any ideas</p> <p>EDIT: this is only for sample data, i only want this to occur when the time stamps the same, otherwise, it will order accordingly as per the time stamp.</p> <p>Sample file:</p> <p><a href="https://www.dropbox.com/s/611psg6qw4nl9pw/ex1.txt?dl=0" rel="nofollow">https://www.dropbox.com/s/611psg6qw4nl9pw/ex1.txt?dl=0</a></p>