# 937. Reorder Data in Log Files

![](/files/-MiQuhP24o-PDeIZdMl3)

![](/files/-MiQukBo8LTgPnCt4WcY)

time: O(nlogn), n is logs content length

space:(n)

```java
class Solution {
    public String[] reorderLogFiles(String[] logs) {
        if(logs == null || logs.length == 0) return null;
        
        Arrays.sort(logs, (a,b) -> {
            String[] aStr = a.split(" ", 2); // string becomes 2 parts, [identifier, content]
            String[] bStr = b.split(" ", 2);
            boolean isADigit = Character.isDigit(aStr[1].charAt(0)); // check content is digit
            boolean isBDigit = Character.isDigit(bStr[1].charAt(0));
            
            if (!isADigit && !isBDigit) { // both string
                if (!aStr[1].equals(bStr[1])) { // if a String != b String return their compare
                    return aStr[1].compareTo(bStr[1]);
                } else {
                    return aStr[0].compareTo(bStr[0]); // a String == b String, use identifier to compare
                }
            } else if (isADigit && isBDigit) { // both digit, maintain original order
                return 0;
            } else if (!isADigit) { // -1 means a put first, so if a is char, put first
                return -1;
            } else {
                return 1;
            }
        });
        return logs;        
    }
    
}

/*
["identifier asdddsdsa fefwef", "identifier 1 2 3 4 4"]  

letter-logs before digital
sorted lexicographically by their contents, 
contents are the same, then sort them lexicographically by their identifiers.

[]
*/
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/string/937.-reorder-data-in-log-files.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
