0

Alright, so I'm supposed to make a script that parses all the chat logs from irc channels, combine the ones that match names, and then sort the chat logs by date.

This is an example chat log:

    jul 29 19:20:53 <lol> lolfile3
    jul 31 19:20:53 <lol> lolfile3321
    aug 1 19:20:53 <lol> lolfile31324
    jul 30 19:20:53 <lol> lolfile32

I need to order them by the timestamp (jul 30 19:20:53) however I've been trying for hours on end but to no avail.

Here's the code I already have (this is file merging, file writing and everything. The timestamp is the last thing I need to do!)

I couldn't figure out how to add code to my question so I link you to pastebin: http://pastebin.com/2VrSRZZr

Thank you so much if you could help me by posting code. Obviously I don't expect to be spoonfed but some code would be nice.

phl0w
  • 17
  • 4
  • You can just copy and past code here. Be sure to put 4 spaces before each line so that it is formatted correctly. – Code-Apprentice Jul 31 '12 at 23:29
  • Also it will help us help you if you can ask a specific question about where you are encountering problems and limit the code to that particular issue. – Code-Apprentice Jul 31 '12 at 23:30
  • I have tried that, but it kept telling me "Oops! It seems you have that has not been formatted correctly." – phl0w Jul 31 '12 at 23:31
  • I am having problems with ordering them by timestamp – phl0w Jul 31 '12 at 23:31
  • Please post just that portion of your code where you are trying to sort the list so we can focus in on the problem. – Code-Apprentice Jul 31 '12 at 23:32
  • I suspect that this website is complaining about some of your long lines of code. You can probably omit most of those since the exact details don't seem necessary to helping you. If you do need them, then you will need to make sure they are all one line or break them up in more appropriate places. – Code-Apprentice Jul 31 '12 at 23:36
  • I have removed all my previous code towards ordering it, I've tried TreeMaps, SortedMaps, Collections#sort, Collections#sort with custom Comparator, and nothing worked ;/ – phl0w Jul 31 '12 at 23:36
  • 1
    1. convert string to date http://stackoverflow.com/a/4216767/176569 2. sort by date http://stackoverflow.com/a/5927408/176569 – bpgergo Jul 31 '12 at 23:37

1 Answers1

0

I'm not entirely sure what the "" in your example is, so this may not be entirely correct, but it shows how to convert the timestamp strings to comparable objects. Hopefully this gets you on the right track.

DateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy")
List<String> lines = readLines(); // use a function of your to read the lines
List<LogEntry> entries = new LinkedList<LogEntry>();
for ( String line : lines ) {
  int splitIndex = line.indexOf("<log>");
  String time = line.substring(0,splitIndex);
  Date date = dateFormat.parse(time);
  entries.add(new LogEntry(date,line.substring(splitIndex));
}

Collections.sort(entries);


// create a class to hold the log contents and the timestamp

class LogEntry implements Comparable<LogEntry> {
    private final Date time;
    private final String entry;

    public void compare(LogEntry other) {
       return time.compareTo(other.time);
    }
}

EDIT: I ran the code you created in pastebin and the output was

Fri Jul 29 17:09:50 EDT 2011 <phl0w> tes
Sun Jul 31 17:08:49 EDT 2011 <yyyy> alewrwae
Sun Jul 31 17:09:50 EDT 2011 <phl0w> tes
Sun Jul 31 17:10:49 EDT 2011 <Andy_> Speed
Sun Jul 31 17:10:51 EDT 2011 <Andy_> lol Speed
Sun Jul 31 17:11:51 EDT 2011 <xxxx> wrkjaer
Sun Jul 31 19:20:50 EDT 2011 <phl0w> lolfile1
Sun Jul 31 19:20:53 EDT 2011 <phl0w> lolfile3
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
  • That doesn't work, I tried that and it still didn't sort them. – phl0w Jul 31 '12 at 23:55
  • Can you post a small example with working code that shows the problem? – Jeff Storey Aug 01 '12 at 00:13
  • I would but it doesn't let me post the code here (only have 550 characters), so I'll link you to the pastebin: http://pastebin.com/P6FjvnMd – phl0w Aug 01 '12 at 00:27
  • Can you include the whole code, including FORMAT? Please just put it into a small main method so I can run it and see exactly the same thing you are seeing. – Jeff Storey Aug 01 '12 at 00:32