# Leetcode: Reorder Log Files

Reorder Log Files

<a href=”https://github.com/dennyzhang/code.dennyzhang.com/tree/master/problems/example“><img align=”right” width=”200″ height=”183″ src=”” /></a>

Similar Problems:

You have an array of logs. Each log is a space delimited string of words.

For each log, the first word in each log is an alphanumeric identifier. Then, either:

• Each word after the identifier will consist only of lowercase letters, or;
• Each word after the identifier will consist only of digits.

We will call these two varieties of logs letter-logs and digit-logs. It is guaranteed that each log has at least one word after its identifier.

Reorder the logs so that all of the letter-logs come before any digit-log. The letter-logs are ordered lexicographically ignoring identifier, with the identifier used in case of ties. The digit-logs should be put in their original order.

Return the final order of the logs.

Example 1:

```Input: ["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"]
```

Note:

1. 0 <= logs.length <= 100
2. 3 <= logs[i].length <= 100
3. logs[i] is guaranteed to have an identifier, and a word after the identifier.

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.

• Solution:
```// Blog link: https://code.dennyzhang.com/example
// Basic Ideas: quicksort
// Complexity: O(n*log(n)), Space O(1)
import ("sort"
"strings")
func reorderLogFiles(logs []string) []string {
sort.SliceStable(logs, func(i, j int) bool {
str1, str2 := "", ""
id1, id2 := "", ""
for k, str := range strings.Split(logs[i], " ") {
if k == 0 {
id1 = str
continue
}
all_digit := true
for _, v := range str {
if v<'0' || v>'9' {
all_digit = false
break
}
}
if all_digit == true { continue }
str1 += str + "A"
}
if str1 == "" { str1 = "{" }
for k, str := range strings.Split(logs[j], " ") {
if k == 0 {
id2 = str
continue
}
all_digit := true
for _, v := range str {
if v<'0' || v>'9' {
all_digit = false
break
}
}
if all_digit == true { continue }
str2 += str + "A"
}
if str2 == "" { str2 = "{" }
if str1 == str2 {
if str1 == "{" {
return i<j
} else {
return id1<id2
}
} else {
return str1 < str2
}
})
return logs
}
```

<div style=”overflow: hidden;”>