#C14121. Sort Employees by Department
Sort Employees by Department
Sort Employees by Department
You are given a list of employees, each with a name, age, department, and salary. Your task is to group the employees by their department and, for each department, output the list of employee names sorted in descending order by salary. In cases where two employees have the same salary, maintain their original order of appearance in the input.
Mathematically, if an employee is represented as an object with properties:
[ \text{employee} = {\text{name},\ \text{age},\ \text{department},\ \text{salary}} ]
Then for each department D, output a list L_D such that:
[ L_D = \text{[names of employees in D sorted by salary in descending order (stable)]} ]
</p>inputFormat
The input is provided via standard input and has the following format:
- The first line contains an integer
N
representing the number of employees. - The next
N
lines each contain the details of an employee in the following order separated by spaces:name age department salary
. Theage
andsalary
are integers.
For example:
4 Alice 30 HR 60000 Bob 25 IT 70000 Charlie 28 HR 62000 David 40 IT 75000
outputFormat
The output should be a single JSON object (printed to standard output) representing a dictionary. Each key is a department name and its corresponding value is a JSON array of employee names sorted by their salary in descending order. For example, the output for the sample input above should be:
{"HR": ["Charlie", "Alice"], "IT": ["David", "Bob"]}## sample
4
Alice 30 HR 60000
Bob 25 IT 70000
Charlie 28 HR 62000
David 40 IT 75000
{"HR": ["Charlie", "Alice"], "IT": ["David", "Bob"]}