> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/string/937.-reorder-data-in-log-files.md).

# 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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
