日志排序
标题:日志排序 | 时间限制:1秒 | 内存限制:262144K |
运维工程师采集到某产品现网运行一天产生的日志N条,现需根据日志时间按时间先后顺序对日志进行排序。
日志时间格式为:
H:M:S.N
H表示小时(0-23),M表示分钟(0-59),S表示秒(0-59),N表示毫秒(0-999)
时间可能并没有补齐,也就是说: 01:01:01.001,也可能表示为1:1:1.1
日志时间格式为:
H:M:S.N
H表示小时(0-23),M表示分钟(0-59),S表示秒(0-59),N表示毫秒(0-999)
时间可能并没有补齐,也就是说: 01:01:01.001,也可能表示为1:1:1.1
import java.util.Comparator; import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextLine()) { int num = Integer.parseInt(sc.nextLine().trim()); LinkedList<String> list = new LinkedList<>(); for (int i = 0; i < num; i++) { list.add(sc.nextLine().trim()); } list.sort(Comparator.comparingLong(Main::calc)); list.forEach(System.out::println); } } private static long calc(String str) { String[] split = str.split(":"); String[] split1 = split[2].split("\\."); int hour = Integer.parseInt(split[0]) * 60 * 60 * 1000; int minute = Integer.parseInt(split[1]) * 60 * 1000; int second = Integer.parseInt(split1[0]) * 1000; int nnn = Integer.parseInt(split1[1]); return hour + minute + second + nnn; } }