-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReorderDataInLogFiles.java
More file actions
47 lines (34 loc) · 1.21 KB
/
ReorderDataInLogFiles.java
File metadata and controls
47 lines (34 loc) · 1.21 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
45
46
47
package Leetcode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class ReorderDataInLogFiles
{
public static void main( String[] args )
{
String[] logs = { "dig1 8 1 5 1", "let1 art can", "dig2 3 6", "let2 own kit dig", "let3 art zero" };
String[] reorderLogFiles = reorderLogFiles( logs );
for( int i = 0; i < reorderLogFiles.length; i++ )
{
System.out.println( reorderLogFiles[i] );
}
}
public static String[] reorderLogFiles( String[] logs )
{
Arrays.sort( logs, ( log1, log2 ) -> {
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 )
{
if( split1[1].compareTo( split2[1] ) != 0 )
return split1[1].compareTo( split2[1] );
return split1[0].compareTo( split2[0] );
}
return isDigit1 ? ( isDigit2 ? 0 : 1 ) : -1;
} );
return logs;
}
}