class SortLogFile {

  public static void sortLogFile(InputStream logFile, PrintWriter sortedFile) throws IOException {

    Collection<LogFileParser.LogEntry> sorted = new TreeSet<>((entry1, entry2) -> {
      String stamp1 = entry1.getTimestamp();
      String stamp2 = entry2.getTimestamp();

      if (stamp1.equals(stamp2)) {
        if (entry1.getContents().equals(entry2.getContents())) {
          // Timestamps and contents are both equal - compare hashCode()
          return Integer.compare(entry1.hashCode(), entry2.hashCode());
        }
        return entry1.getContents().compareTo(entry2.getContents());
      }
      return stamp1.compareTo(stamp2);
    });

    BufferedReader br = new BufferedReader(new InputStreamReader(logFile));
    LogFileParser parser = new LogFileParser(null, br);
    while (parser.hasMoreEntries()) {
      sorted.add(parser.getNextEntry());
    }

    for (LogFileParser.LogEntry entry : sorted) {
      entry.writeTo(sortedFile);
    }
  }

}