#C12125. Custom String Sorting
Custom String Sorting
Custom String Sorting
You are given a list of strings. Your task is to sort them by the following rules:
- Sort primarily in descending order by their length.
- If two strings have the same length, sort them in lexicographical (dictionary) order (i.e. the one that comes first in alphabetical order appears first).
Formally, given a list of strings \(S = [s_1, s_2, \dots, s_n]\), you should order them such that for any two strings \(a\) and \(b\):
\[ \text{if } |a| \neq |b|, \text{ then } |a| > |b| \text{ implies } a \text{ comes before } b, \] \[ \text{if } |a| = |b|, \text{ then } a < b \text{ in lexicographical order.} \]This problem tests your ability to combine sorting criteria. Good luck!
inputFormat
The input is provided via standard input (stdin) in the following format:
n s1 s2 ... sn
Here, \(n\) (an integer) denotes the number of strings, and each of the following \(n\) lines contains one string. Note that a string can be empty.
outputFormat
Output the sorted strings to standard output (stdout), one per line. The strings are sorted primarily by descending length and secondarily by lexicographical order (for those with the same length).
## sample7
apple
banana
cherry
date
egg
fig
grape
banana
cherry
apple
grape
date
egg
fig
</p>