我不完全确定您示例中的“”是什么,因此这可能不完全正确,但它显示了如何将时间戳字符串转换为可比较的对象。希望这能让你走上正确的轨道。
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);
}
}
编辑:我运行了您在 pastebin 中创建的代码,输出为
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