I have a list of dictionaries that I'm trying to sort. This is what I have so far:
output_list = [{'mac': '0123.4567.89ab', 'port': 'Gi1/0/10'},
{'mac': '0123.4567.89ab', 'port': 'Gi1/0/1'},
{'mac': '0123.4567.89ab', 'port': 'Gi1/0/5'},
{'mac': '0123.4567.89ab', 'port': 'Gi1/0/48'},
{'mac': '0123.4567.89ab', 'port': 'Gi2/0/1'},
{'mac': '0123.4567.89ab', 'port': 'Gi1/0/6'},
{'mac': '0123.4567.89ab', 'port': 'Gi2/0/4'},
{'mac': '0123.4567.89ab', 'port': 'Gi2/0/13'},
{'mac': '0123.4567.89ab', 'port': 'Gi8/0/9'},
{'mac': '0123.4567.89ab', 'port': 'Gi8/0/8'},
{'mac': '0123.4567.89ab', 'port': 'Te1/1/1'}]
mac_list = sorted(output_list, key=lambda d: "/".join([x for x in d['port'].split("/")]))
pprint(mac_list)
However, it's not sorting the dictionaries the way I want.
"C:\Program Files\Python310\python.exe" "C:/Scripts/Python/test1.py"
[{'mac': '0123.4567.89ab', 'port': 'Gi1/0/1'},
{'mac': '0123.4567.89ab', 'port': 'Gi1/0/10'},
{'mac': '0123.4567.89ab', 'port': 'Gi1/0/48'},
{'mac': '0123.4567.89ab', 'port': 'Gi1/0/5'},
{'mac': '0123.4567.89ab', 'port': 'Gi1/0/6'},
{'mac': '0123.4567.89ab', 'port': 'Gi2/0/1'},
{'mac': '0123.4567.89ab', 'port': 'Gi2/0/13'},
{'mac': '0123.4567.89ab', 'port': 'Gi2/0/4'},
{'mac': '0123.4567.89ab', 'port': 'Gi8/0/8'},
{'mac': '0123.4567.89ab', 'port': 'Gi8/0/9'},
{'mac': '0123.4567.89ab', 'port': 'Te1/1/1'}]
Process finished with exit code 0
How can I get it to sort it so it looks like this:
[{'mac': '0123.4567.89ab', 'port': 'Gi1/0/1'},
{'mac': '0123.4567.89ab', 'port': 'Gi1/0/5'},
{'mac': '0123.4567.89ab', 'port': 'Gi1/0/6'},
{'mac': '0123.4567.89ab', 'port': 'Gi1/0/10'},
{'mac': '0123.4567.89ab', 'port': 'Gi1/0/48'},
{'mac': '0123.4567.89ab', 'port': 'Gi2/0/1'},
{'mac': '0123.4567.89ab', 'port': 'Gi2/0/4'},
{'mac': '0123.4567.89ab', 'port': 'Gi2/0/13'},
{'mac': '0123.4567.89ab', 'port': 'Gi8/0/8'},
{'mac': '0123.4567.89ab', 'port': 'Gi8/0/9'},
{'mac': '0123.4567.89ab', 'port': 'Te1/1/1'}]