| 30 | } |
| 31 | |
| 32 | func compareLogs(a, b string) int { |
| 33 | aTokens := strings.Split(a, " ") |
| 34 | bTokens := strings.Split(b, " ") |
| 35 | aIsDigit, _ := regexp.MatchString("[0-9]", aTokens[1]) |
| 36 | bIsDigit, _ := regexp.MatchString("[0-9]", bTokens[1]) |
| 37 | |
| 38 | // don't move anything if a and b are digits |
| 39 | // don't move back b if it's a digit and a is not |
| 40 | if (aIsDigit && bIsDigit) || (!aIsDigit && bIsDigit) { |
| 41 | return -1 |
| 42 | } |
| 43 | |
| 44 | // move back b if a is a digit and b is not |
| 45 | if aIsDigit && !bIsDigit { |
| 46 | return 1 |
| 47 | } |
| 48 | |
| 49 | // both a & b aren't digit logs |
| 50 | aLetters := strings.TrimPrefix(a, aTokens[0]+" ") |
| 51 | bLetters := strings.TrimPrefix(b, bTokens[0]+" ") |
| 52 | if aLetters == bLetters { |
| 53 | // if aLetter and bLetters are equal, compare identifiers |
| 54 | return strings.Compare(aTokens[0], bTokens[0]) |
| 55 | } |
| 56 | |
| 57 | // compare letters |
| 58 | return strings.Compare(aLetters, bLetters) |
| 59 | } |