0

I am trying to sort below dictionary based on "resume_match_score" in descending order.

{'16334': [{'skill_match': {'java': 33,
    'python': 5,
    'swing': 1,
    'apache cassandra': 1},
   'skill_match_score': 0.8},
  {'doc_attachment': '97817_1560102392mahesh-java.docx',
   'document_path': '06_2019',
   'firstname': 'nan',
   'lastname': 'nan'},
  {'job_title_match': {'java developer': 3}, 'job_title_match_score': 0.5},
  {'resume_match_score': 0.71}],
 '4722': [{'skill_match': {'java': 24, 'python': 1, 'hadoop': 31},
   'skill_match_score': 0.6},
  {'doc_attachment': '4285_1560088607Srujan_Hadoop.docx',
   'document_path': '06_2019',
   'firstname': 'nan',
   'lastname': 'nan'},
  {'job_title_match': {'hadoop developer': 3, 'java developer': 2},
   'job_title_match_score': 1.0},
  {'resume_match_score': 0.72}]

I tried as below and this seems to be working but giving only key instead of full dictionary object.

result = sorted(test_d, key=lambda k: test_d[k][3].get("resume_match_score", 0), reverse=True)

and

result = ['4722', '16334']

How to get complete dictionary in sorted order based on key resume_match_score?

Thanks in advance.

Om Prakash
  • 2,675
  • 4
  • 29
  • 50
  • two issues. first, use `sorted(test_d.items()..` instead of just `test_d` if you need both keys and values. Secondly, that kind of sorting will give a list of tuples, so you'd have to convert that back to a dictionary. Historically, dictionaries were unordered, so ideally you'd still want to consider using OrderedDict instead. But if you're using python 3.7+ you're fine to just use dicts – Paritosh Singh Jul 05 '19 at 12:11
  • did you know that dict is unordered structure? – RomanPerekhrest Jul 05 '19 at 12:11
  • 2
    Possible duplicate of [How do I sort a dictionary by value?](https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value) – glibdud Jul 05 '19 at 12:11

1 Answers1

2

I am not sure if this is the proper way to do it but you can go through a for loop and change every key with a tuple of (key, value) pair.

Basically you can just add by something like this:

real_result = []
for key in result:
    real_result.append((key, dict.get(key))
Safak Ozdek
  • 906
  • 11
  • 18