Build a tiny version of the Unix sort command. Given a list of lines, return them sorted in ascending order by character code (like LC_ALL=C sort: uppercase letters come before lowercase ones, "Zebra" before "apple", and an empty line comes first), joined by newline characters (\n). Duplicates are all kept. If the list is empty, return the empty string.
Function signature:
public String sortLines(List<String> lines)
Example:
Input: lines = ["banana", "apple", "cherry"] Output: "apple\nbanana\ncherry" When printed: apple banana cherry
Copy the input into a fresh
ArrayList, sort it with Collections.sort, then String.join.
The full solution:
class Solution {
public String sortLines(List<String> lines) {
List<String> sortedLines = new ArrayList<>(lines);
Collections.sort(sortedLines);
return String.join("\n", sortedLines);
}
}
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
