-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathReorderDataInLogFiles.java
More file actions
44 lines (40 loc) · 1.35 KB
/
ReorderDataInLogFiles.java
File metadata and controls
44 lines (40 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.dbc;
import java.util.Arrays;
public class ReorderDataInLogFiles {
class Pair {
String log;
int index;
public Pair(String log, int index) {
this.log = log;
this.index = index;
}
}
public int logCompare(Pair pair1, Pair pair2) {
String log1 = pair1.log, log2 = pair2.log;
int index1 = pair1.index, index2 = pair2.index;
String[] split1 = log1.split(" ", 2);
String[] split2 = log2.split(" ", 2);
boolean isDigit1 = Character.isDigit(split1[1].charAt(0));
boolean isDigit2 = Character.isDigit(split2[1].charAt(0));
if (isDigit1 && isDigit2) return index1 - index2;
if (!isDigit1 && !isDigit2) {
int sc = split1[1].compareTo(split2[1]);
if (sc != 0) return sc;
return split1[0].compareTo(split2[0]);
}
return isDigit1 ? 1 : -1;
}
public String[] reorderLogFiles(String[] logs) {
int length = logs.length;
Pair[] arr = new Pair[length];
for (int i = 0; i < length; i++) {
arr[i] = new Pair(logs[i], i);
}
Arrays.sort(arr, (a, b) -> logCompare(a, b));
String[] reordered = new String[length];
for (int i = 0; i < length; i++) {
reordered[i] = arr[i].log;
}
return reordered;
}
}