So you have a dictionary after all!
Dictionaries are indexed by key and the keys are in a hash order for fast lookups. They will always display in this order! If you want to sort by a certain item, you have to convert them to lists. I thought that is what you did.
data_dict = {
'1234': ['Matt', '2.5', 'CS'],
'1000': ['John', '4.0', 'Music'],
'1023': ['Aaron', '3.1', 'PreMed'],
'1001': ['Paul', '3.9', 'Music'],
'9000': ['Kris', '3.5', 'Business']
}
# convert dictionary to a list of tuples
data_list = [(key, val) for key, val in data_dict.items()]
print(data_list)
"""the raw list -->
[
('1234', ['Matt', '2.5', 'CS']),
('9000', ['Kris', '3.5', 'Business']),
('1001', ['Paul', '3.9', 'Music']),
('1023', ['Aaron', '3.1', 'PreMed']),
('1000', ['John', '4.0', 'Music'])
]
"""
# now you can sort the list by item[1][1]
sorted_list = sorted(data_list, key=lambda tup: tup[1][1])
print(sorted_list)
"""result sorted by item[1][1] -->
[
('1234', ['Matt', '2.5', 'CS']),
('1023', ['Aaron', '3.1', 'PreMed']),
('9000', ['Kris', '3.5', 'Business']),
('1001', ['Paul', '3.9', 'Music']),
('1000', ['John', '4.0', 'Music'])]
"""
# however if you go back to a dictionary
new_dict = dict(sorted_list)
print(new_dict)
"""result is the dictionary order again -->
{
'1234': ['Matt', '2.5', 'CS'],
'1000': ['John', '4.0', 'Music'],
'1001': ['Paul', '3.9', 'Music'],
'1023': ['Aaron', '3.1', 'PreMed'],
'9000': ['Kris', '3.5', 'Business']}
"""
So, if you want to do any processing that needs a sorted order, to have to convert your dictionary to a temporary list.