Problem overview
This is the first version of a small sort command. The function sortLines takes a slice of strings and returns them in ascending order, joined by "\n". Duplicates are all kept, and an empty slice gives the empty string.
The order is by character code, like LC_ALL=C sort. In that order every uppercase letter comes before every lowercase one, and an empty line comes before everything else. The example ["banana", "apple", "cherry"] becomes "apple\nbanana\ncherry".
Approach: copy, sort, join
Go's standard library does the hard part. sort.Strings sorts a []string in ascending order, comparing strings byte by byte. For ASCII text that is exactly "by character code", so no custom comparison is needed.
One thing needs care: sort.Strings sorts in place. A slice is a small header pointing at an array, so the caller's slice and the one you receive share the same elements. Sorting lines directly would reorder the caller's data as a side effect. The function therefore sorts a copy.
The last step turns the slice into one string. strings.Join places the separator between elements only, so there is no trailing newline.
Step by step
- Write the signature
sortLines(lines []string) stringwith its closing brace. - Make a copy named
sortedLinesby appending all oflinesto a new, empty slice. The trailing...spreads a slice into separate arguments, soappendadds every element. - Sort the copy in place with
sort.Strings. - Join with
"\n"and return the result.
The solution in Go
func sortLines(lines []string) string {
sortedLines := append([]string{}, lines...)
sort.Strings(sortedLines)
return strings.Join(sortedLines, "\n")
}
The details that matter:
- Why the copy is needed. Assigning one slice to another does not copy elements. With
orig := []string{"c", "a", "b"}andalias := orig, callingsort.Strings(alias)leavesorigprinting as[a b c]. Appending onto a fresh empty slice allocates new storage, so sorting it leaveslinesas it was. append([]string{}, lines...)is a common idiom for cloning a slice. The empty literal supplies a new array, andlines...passes every element as a separate argument.- The imports. The function uses the
sortandstringspackages. Go reports an unused import as an error, so import only what the code calls. - Empty input. Copying an empty slice gives an empty slice, sorting it does nothing, and
strings.Joinof an empty slice returns"", which is the required result.
Complexity
Time: O(n log n) comparisons for n lines, from the sort. Each comparison can cost up to the length of the strings compared, so the true cost also depends on how long the lines are. Copying and joining are O(n) in the number of lines.
Space: O(n). The copy holds n string headers, and the joined result is a new string as long as all the lines together. The sort itself works in place.
Edge cases and common mistakes
- Uppercase before lowercase.
["apple", "Zebra"]sorts to"Zebra\napple", becauseZhas a smaller code thana. Sorting case-insensitively would be a different tool. - Empty lines come first.
["b", "", "a"]gives"\na\nb", so the output starts with a newline. - Duplicates stay.
["b", "a", "b"]gives"a\nb\nb". Sorting is not de-duplicating. - A prefix sorts first.
["app", "apple", "ap"]gives"ap\napp\napple": a shorter string that is a prefix of a longer one is smaller. - Sorting the input directly. It passes the examples but reorders the caller's slice, which can surprise any code that still uses it.
- Expecting a trailing newline.
strings.Joinadds none after the last line.
Testing
["banana", "apple", "cherry"]gives"apple\nbanana\ncherry".["apple", "Zebra"]gives"Zebra\napple".["b", "", "a"]gives"\na\nb".["b", "a", "b"]gives"a\nb\nb".["app", "apple", "ap"]gives"ap\napp\napple".- An empty slice gives
"".
