MCPcopy Create free account
hub / github.com/austingebauer/go-leetcode / compareLogs

Function compareLogs

reorder_data_in_log_files_937/solution.go:32–59  ·  view source on GitHub ↗
(a, b string)

Source from the content-addressed store, hash-verified

30}
31
32func 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}

Callers 1

reorderLogFilesFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected