1

I have a dictionary:

dd = dd = {'start1': 'geo!-23.7338,-46.7876',
 'start2': 'geo!-23.7338,-46.7876',
 'start3': 'geo!-23.7338,-46.7876',
 'start4': 'geo!-23.7338,-46.7876',
 'start5': 'geo!-23.7338,-46.7876',
 'start6': 'geo!-23.7338,-46.7876',
 'start7': 'geo!-23.7338,-46.7876',
 'start8': 'geo!-23.7338,-46.7876',
 'start9': 'geo!-23.7338,-46.7876',
 'start10':'geo!-23.7338,-46.7876',
 'start11':'geo!-23.7338,-46.7876',
 'start12':'geo!-23.7338,-46.7876',
 'start13':'geo!-23.7338,-46.7876',
 'start0': 'geo!-23.7338,-46.7876'}

I want to sort this dictionary with respect to key so it looks like:

dd =  {'start0': 'geo!-23.7338,-46.7876',
 'start1': 'geo!-23.7338,-46.7876',
 'start2': 'geo!-23.7338,-46.7876',
 'start3': 'geo!-23.7338,-46.7876',
 'start4': 'geo!-23.7338,-46.7876',
 'start5': 'geo!-23.7338,-46.7876',
 'start6': 'geo!-23.7338,-46.7876',
 'start7': 'geo!-23.7338,-46.7876',
 'start8': 'geo!-23.7338,-46.7876',
 'start9': 'geo!-23.7338,-46.7876',
 'start10':'geo!-23.7338,-46.7876',
 'start11':'geo!-23.7338,-46.7876',
 'start12':'geo!-23.7338,-46.7876',
 'start13':'geo!-23.7338,-46.7876'}

I've been trying to use the following code:

import collections

dd = collections.OrderedDict(sorted(start_points.items()))
dd = dict(start_points)

But this is what I'm getting:

{'start0': 'geo!-23.7338,-46.7876',
 'start1': 'geo!-23.7338,-46.7876',
 'start10':'geo!-23.7338,-46.7876',
 'start11':'geo!-23.7338,-46.7876',
 'start12':'geo!-23.7338,-46.7876',
 'start13':'geo!-23.7338,-46.7876',
 'start2': 'geo!-23.7338,-46.7876',
 'start3': 'geo!-23.7338,-46.7876',
 'start4': 'geo!-23.7338,-46.7876',
 'start5': 'geo!-23.7338,-46.7876',
 'start6': 'geo!-23.7338,-46.7876',
 'start7': 'geo!-23.7338,-46.7876',
 'start8': 'geo!-23.7338,-46.7876',
 'start9': 'geo!-23.7338,-46.7876'}

If anyone knows how to do this I would really appreciate your help.

brenda
  • 656
  • 8
  • 24
  • Does this answer your question? [Python analog of PHP's natsort function (sort a list using a "natural order" algorithm)](https://stackoverflow.com/questions/2545532/python-analog-of-phps-natsort-function-sort-a-list-using-a-natural-order-alg) –  Mar 02 '21 at 20:18
  • Not really. That is a list and I'm trying to sort a dict. I've tried the solutions and the outputs are just the keys of my dictionary as a list. It gets rid off dict values. – brenda Mar 02 '21 at 20:29

1 Answers1

1

For your use case, the following code should do the job:

 dd = {'start1': 'geo!-23.7338,-46.7876',
 'start2': 'geo!-23.7277,-46.7609',
 'start3': 'geo!-23.7244,-46.7556',
 'start4': 'geo!-23.7247,-46.7546',
 'start5': 'geo!-23.7251,-46.7606',
 'start6': 'geo!-23.7234,-46.7645',
 'start7': 'geo!-23.7181,-46.7644',
 'start8': 'geo!-23.6992,-46.7691',
 'start9': 'geo!-23.7006,-46.7678',
 'start10': 'geo!-23.69,-46.768',
 'start11': 'geo!-23.6909,-46.7688',
 'start12': 'geo!-23.6885,-46.7691',
 'start13': 'geo!-23.6854,-46.7702',
 'start0': 'geo!-23.6702,-46.6954'}
 
 
import collections

dd = collections.OrderedDict(sorted(dd.items(), key=lambda x: int(x[0][5:])))
dd = dict(dd)
print(dd)
Kumar Arnav
  • 301
  • 2
  • 13