i have a list of dict's eg
list = [{'route number': '996', 'happy customers': '0'}, {'route number': '123', 'happy customers': '4'}, {'route number': '321', 'happy customers': '0.00'}, {'route number': '456', 'happy customers': '5'}, {'route number': '856', 'happy customers': '8.760'}, {'route number': '555', 'happy customers': '1.1'}, {'route number': '565', 'happy customers': '0'}, {'route number': '784', 'happy customers': '3.010'}]
and i am trying to sort them by happy customers and if happy customers == 0 then sort by route number i have tried this:
sorted_list = sorted(list, key = operator.itemgetter ("happy customers", "route number"), reverse = True)
output is
[{'route number': '856', 'happy customers': '8.760'}, {'route number': '456', 'happy customers': '5'}, {'route number': '123', 'happy customers': '4'}, {'route number': '784', 'happy customers': '3.010'}, {'route number': '555', 'happy customers': '1.1'}, {'route number': '321', 'happy customers': '0.00'}, {'route number': '996', 'happy customers': '0'}, {'route number': '565', 'happy customers': '0'}]
expected output:
[{'route number': '856', 'happy customers': '8.760'}, {'route number': '456', 'happy customers': '5'}, {'route number': '123', 'happy customers': '4'}, {'route number': '784', 'happy customers': '3.010'}, {'route number': '555', 'happy customers': '1.1'}, {'route number': '321', 'happy customers': '0.00'}, {'route number': '565', 'happy customers': '0'}, {'route number': '996', 'happy customers': '0'}]
i think it has something to do with
route number': '321', 'happy customers': '0.00'}
number on the end being a float 0.00 not just an int 0 but i dont know how to change it to make it work any help would be appreciated FYI list of dict's imported from a routes.txt file hence some numbers floats and some int's any ideas on how to make this work ?? or what i am doing wrong with the code