#C12991. Sort Strings by Length
Sort Strings by Length
Sort Strings by Length
You are given a list of strings. Your task is to sort the strings primarily by their lengths (from shortest to longest). In the case of strings having an equal length, they should be sorted in lexicographic (dictionary) order.
Note: Strings can be empty.
Sorting Criterion: For two strings \(s_1\) and \(s_2\), we define the ordering based on the tuple \((|s|, s)\). That is, \(s_1\) comes before \(s_2\) if either \(|s_1| < |s_2|\) or \(|s_1| = |s_2|\) and \(s_1 < s_2\) in lexicographic order.
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains an integer \(N\) — the number of strings.
- The next \(N\) lines each contain one string. These strings may be empty.
outputFormat
Output the sorted list of strings to standard output (stdout), with each string printed on a separate line. The strings should be sorted by their lengths in ascending order, and for strings of equal length, in lexicographic order.
## sample4
apple
pie
banana
cake
pie
cake
apple
banana
</p>