Advertisement
phl0w

Untitled

Jul 31st, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1.         List<LogEntryComparator> entries = new LinkedList<LogEntryComparator>();
  2.         Date date = null;
  3.         for (String line : list) { //list is an arraylist of strings.
  4.             String time = "2011 " + line.substring(0, 15);
  5.             try {
  6.                 date = FORMAT.parse(time);
  7.             } catch (Exception e) {
  8.                 e.printStackTrace();
  9.             }
  10.             entries.add(new LogEntryComparator(date, line.substring(15)));
  11.         }
  12.         Collections.sort(entries);
  13.  
  14. ...
  15.  
  16. import java.util.Date;
  17.  
  18. public class LogEntryComparator implements Comparable<LogEntryComparator> {
  19.     Date time = null;
  20.     String entry = "";
  21.  
  22.     public LogEntryComparator(Date date, String entry) {
  23.         this.time = date;
  24.         this.entry = entry;
  25.     }
  26.  
  27.     @Override
  28.     public int compareTo(LogEntryComparator le1) {
  29.         return time.compareTo(le1.time);
  30.     }
  31. }
  32.  
  33. result (as you can see unsorted)
  34. Sun Jul 31 17:09:50 CEST 2011 <phl0w> tes
  35. Sun Jul 31 17:10:49 CEST 2011 <Andy_> Speed
  36. Sun Jul 31 17:10:51 CEST 2011 <Andy_> lol Speed
  37. Sun Jul 31 19:20:50 CEST 2011 <phl0w> lolfile1
  38. Fri Jul 29 17:09:50 CEST 2011 <phl0w> tes
  39. Sun Jul 31 17:08:49 CEST 2011 <Andy_> Speed
  40. Sun Jul 31 17:11:51 CEST 2011 <Andy_> lol Speed
  41. Sun Jul 31 19:20:53 CEST 2011 <phl0w> lolfile3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement