## Algorithm

Problem Name: 937. Reorder Data in Log Files

You are given an array of `logs`. Each log is a space-delimited string of words, where the first word is the identifier.

There are two types of logs:

• Letter-logs: All words (except the identifier) consist of lowercase English letters.
• Digit-logs: All words (except the identifier) consist of digits.

Reorder these logs so that:

1. The letter-logs come before all digit-logs.
2. The letter-logs are sorted lexicographically by their contents. If their contents are the same, then sort them lexicographically by their identifiers.
3. The digit-logs maintain their relative ordering.

Return the final order of the logs.

Example 1:

```Input: logs = ["dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"]
Output: ["let1 art can","let3 art zero","let2 own kit dig","dig1 8 1 5 1","dig2 3 6"]
Explanation:
The letter-log contents are all different, so their ordering is "art can", "art zero", "own kit dig".
The digit-logs have a relative order of "dig1 8 1 5 1", "dig2 3 6".
```

Example 2:

```Input: logs = ["a1 9 2 3 1","g1 act car","zo4 4 7","ab1 off key dog","a8 act zoo"]
Output: ["g1 act car","a8 act zoo","ab1 off key dog","a1 9 2 3 1","zo4 4 7"]
```

Constraints:

• `1 <= logs.length <= 100`
• `3 <= logs[i].length <= 100`
• All the tokens of `logs[i]` are separated by a single space.
• `logs[i]` is guaranteed to have an identifier and at least one word after the identifier.

## Code Examples

### #1 Code Example with Java Programming

```Code - Java Programming```

``````
class Solution {
public String[] reorderLogFiles(String[] logs) {
List letterLogs = new ArrayList<>();
List digitLogs = new ArrayList<>();
for (String log : logs) {
if (Character.isDigit(log.split("\\s+").charAt(0))) {
} else {
}
}
letterLogs.sort(Comparator.comparing((String o) -> o.substring(o.indexOf(' ') + 1))
.thenComparing(o -> o.split("\\s+")));
return letterLogs.toArray(new String);
}
}
``````
Copy The Code &

Input

cmd
logs = ["dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"]

Output

cmd
["let1 art can","let3 art zero","let2 own kit dig","dig1 8 1 5 1","dig2 3 6"]

### #2 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
const reorderLogFiles = function(logs) {
const letterLog = [],
digitLog = []
for (let log of logs) {
if (isNaN(log.split(' '))) {
letterLog.push(log)
} else {
digitLog.push(log)
}
}
letterLog.sort((log1, log2) => {
let body1 = log1.slice(log1.indexOf(' '))
let body2 = log2.slice(log2.indexOf(' '))
if (body1 === body2) {
return log1.split(' ') > log2.split(' ') ? 1 : -1
} else {
return body1 > body2 ? 1 : -1
}
})
return [...letterLog, ...digitLog]
}
``````
Copy The Code &

Input

cmd
logs = ["dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"]

Output

cmd
["let1 art can","let3 art zero","let2 own kit dig","dig1 8 1 5 1","dig2 3 6"]

### #3 Code Example with Python Programming

```Code - Python Programming```

``````
class Solution:
def reorderLogFiles(self, logs):
return sorted(filter(lambda l: l[l.find(" ") + 1].isalpha(), logs), key = lambda x: (x[x.find(" "):], x[:x.find(" ")])) + list(filter(lambda l: l[l.find(" ") + 1].isdigit(), logs))
``````
Copy The Code &

Input

cmd
logs = ["a1 9 2 3 1","g1 act car","zo4 4 7","ab1 off key dog","a8 act zoo"]

Output

cmd
["g1 act car","a8 act zoo","ab1 off key dog","a1 9 2 3 1","zo4 4 7"]

### #4 Code Example with C# Programming

```Code - C# Programming```

``````
using System.Collections.Generic;

namespace LeetCode
{
public class _0937_ReorderDataInLogFiles
{
public string[] ReorderLogFiles(string[] logs)
{
if (logs == null || logs.Length == 0) return new string;

List letterLogs = new List();
List digitLogs = new List();
{
if (char.IsDigit(log[log.IndexOf(' ') + 1])) digitLogs.Add(log);
}

letterLogs.Sort((a, b) =>
{
string sub1 = a.Substring(a.IndexOf(' ') + 1);
string sub2 = b.Substring(b.IndexOf(' ') + 1);

int result = sub1.CompareTo(sub2);
if (result == 0)
result = a.Substring(0, a.IndexOf(' ')).CompareTo(b.Substring(0, b.IndexOf(' ')));

return result;
});

return letterLogs.ToArray();
}
}
}
``````
Copy The Code &

Input

cmd
logs = ["a1 9 2 3 1","g1 act car","zo4 4 7","ab1 off key dog","a8 act zoo"]

Output

cmd
["g1 act car","a8 act zoo","ab1 off key dog","a1 9 2 3 1","zo4 4 7"]