#C212. Sort Objects Based on Multiple Attributes
Sort Objects Based on Multiple Attributes
Sort Objects Based on Multiple Attributes
You are given a list of objects and a list of attribute names which denotes the sorting order. Each object has three attributes: name
(a string), age
(an integer), and score
(an integer). Your task is to sort the objects in increasing order based on the specified attributes. The sorting is stable and follows the order of keys provided. In other words, if the sorting order is given as \(k_1, k_2, \ldots, k_m\), then for any two objects \(a\) and \(b\), the comparison is made using the tuple \((a[k_1], a[k_2], \ldots, a[k_m])\) versus \((b[k_1], b[k_2], \ldots, b[k_m])\) using the usual order relation.
It is guaranteed that all objects contain the keys name
, age
, and score
.
inputFormat
The input is read from standard input (stdin
) and has the following format:
n m key1 key2 ... keym object1 object2 ... objectn
Here,
n
is the number of objects.m
is the number of sorting keys.- The next line contains
m
space-separated strings providing the sorting order. Each key is one ofname
,age
, orscore
. - Each of the next
n
lines contains an object represented by three space-separated values:name
(a string),age
(an integer) andscore
(an integer).
outputFormat
Output to standard output (stdout
) the sorted list of objects. Each object should be printed on a separate line in the format:
name age score
The objects must appear in the sorted order as determined by the provided sorting keys.
## sample3
1
name
John 25 90
Alice 30 85
Bob 22 95
Alice 30 85
Bob 22 95
John 25 90
</p>