0

all.

I need help with dictionary ordering issue.

I have dictionary that I want sort by inner dictionary value. In my case I need sort dictionary by 'ordering' value

{'MotoType': {'verbos_name': 'Тип', 'ordering': 6, 'filtering': False, 'value': 'Кросс'}, 'MarkaMoto': {'verbos_name': 'Марка', 'ordering': 1, 'filtering': True, 'value': 'Honda'}, 'Condition': {'verbos_name': 'Состояние', 'ordering': 5, 'filtering': False, 'value': 'Среднее'}, 'Color': {'verbos_name': 'Цвет', 'ordering': 99, 'filtering': False, 'value': 'Красный'}, 'probeg': {'verbos_name': 'Пробег км.', 'ordering': 6, 'filtering': False, 'value': '34500'}, 'CreatYear': {'verbos_name': 'Год выпуска', 'ordering': 6, 'filtering': True, 'value': '2010'}, 'ModelMoto': {'verbos_name': 'Модель', 'ordering': 2, 'filtering': True, 'value': 'VFR800FI'}}
Oleg
  • 777
  • 1
  • 6
  • 10
  • 3
    Dictionaries don't have order. You should check [OrderedDict](https://docs.python.org/2/library/collections.html#collections.OrderedDict). – Lafexlos Dec 09 '16 at 12:29
  • In Python 3.6 the order of dictionaries will be preserved (see https://docs.python.org/3.6/whatsnew/3.6.html), but they say you still shouldn't rely on this. Think of a dictionary as a hash table. – Daniel Porteous Dec 09 '16 at 12:34
  • Possible duplicate of [Sort a Python dictionary by value](http://stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value) – Łukasz Rogalski Dec 09 '16 at 12:38

2 Answers2

2

Dictionaries are unordered in Python. You can use OrderedDict, which preserves ordering.

from collections import OrderedDict

a = OrderedDict({'MotoType': {'verbos_name': 'Тип', 'ordering': 6, 'filtering': False, 'value': 'Кросс'},
     'MarkaMoto': {'verbos_name': 'Марка', 'ordering': 1, 'filtering': True, 'value': 'Honda'},
     'Condition': {'verbos_name': 'Состояние', 'ordering': 5, 'filtering': False, 'value': 'Среднее'},
     'Color': {'verbos_name': 'Цвет', 'ordering': 99, 'filtering': False, 'value': 'Красный'},
     'probeg': {'verbos_name': 'Пробег км.', 'ordering': 6, 'filtering': False, 'value': '34500'},
     'CreatYear': {'verbos_name': 'Год выпуска', 'ordering': 6, 'filtering': True, 'value': '2010'},
     'ModelMoto': {'verbos_name': 'Модель', 'ordering': 2, 'filtering': True, 'value': 'VFR800FI'}})

out = sorted(a.items(), key=lambda i: i[1]["value"])

print(out)

which outputs

[('CreatYear', {'value': '2010', 'filtering': True, 'ordering': 6, 'verbos_name': 'Год выпуска'}), ('probeg', {'value': '34500', 'filtering': False, 'ordering': 6, 'verbos_name': 'Пробег км.'}), ('MarkaMoto', {'value': 'Honda', 'filtering': True, 'ordering': 1, 'verbos_name': 'Марка'}), ('ModelMoto', {'value': 'VFR800FI', 'filtering': True, 'ordering': 2, 'verbos_name': 'Модель'}), ('Color', {'value': 'Красный', 'filtering': False, 'ordering': 99, 'verbos_name': 'Цвет'}), ('MotoType', {'value': 'Кросс', 'filtering': False, 'ordering': 6, 'verbos_name': 'Тип'}), ('Condition', {'value': 'Среднее', 'filtering': False, 'ordering': 5, 'verbos_name': 'Состояние'})]
Filip Haglund
  • 13,919
  • 13
  • 64
  • 113
2

sorted the data and use OrderedDict keeps the order.

>>> a ={'MotoType': {'verbos_name': 'Тип', 'ordering': 6, 'filtering': False, 'value': 'Кросс'}, 'MarkaMoto': {'verbos_name': 'Марка', 'ordering': 1, 'filtering': True, 'value': 'Honda'}, 'Condition': {'verbos_name': 'Состояние', 'ordering': 5, 'filtering': False, 'value': 'Среднее'}, 'Color': {'verbos_name': 'Цвет', 'ordering': 99, 'filtering': False, 'value': 'Красный'}, 'probeg': {'verbos_name': 'Пробег км.', 'ordering': 6, 'filtering': False, 'value': '34500'}, 'CreatYear': {'verbos_name': 'Год выпуска', 'ordering': 6, 'filtering': True, 'value': '2010'}, 'ModelMoto': {'verbos_name': 'Модель', 'ordering': 2, 'filtering': True, 'value': 'VFR800FI'}}
>>> from collections import OrderedDict
>>> OrderedDict(sorted(a.items(), key=lambda t:t[1]['ordering']))
OrderedDict([('MarkaMoto', {'value': 'Honda', 'filtering': True, 'verbos_name': 'Марка', 'ordering': 1}), ('ModelMoto', {'value': 'VFR800FI', 'filtering': True, 'verbos_name': 'Модель', 'ordering': 2}), ('Condition', {'value': 'Среднее', 'filtering': False, 'verbos_name': 'Состояние', 'ordering': 5}), ('probeg', {'value': '34500', 'filtering': False, 'verbos_name': 'Пробег км.', 'ordering': 6}), ('MotoType', {'value': 'Кросс', 'filtering': False, 'verbos_name': 'Тип', 'ordering': 6}), ('CreatYear', {'value': '2010', 'filtering': True, 'verbos_name': 'Год выпуска', 'ordering': 6}), ('Color', {'value': 'Красный', 'filtering': False, 'verbos_name': 'Цвет', 'ordering': 99})])
>>> 
Fujiao Liu
  • 2,195
  • 2
  • 24
  • 28