Answer the question
In order to leave comments, you need to log in
How to sort a dictionary of dictionaries where key is an object?
I have a dictionary like this:
{
account.account(354,
): {
'period_credit': 0.0,
'code': u'27101',
'period_debit': 100.0,
'name': u'\u0160B LT577180900027467092',
'start_balance': 0.0,
'end_credit': 0.0,
'start_credit': 0.0,
'end_debit': 100.0,
'start_debit': 0.0,
'partner_dicts': {
res.partner(225485,
): {
'period_credit': 0.0,
'code': u'27101',
'period_debit': 100.0,
'name': u'jonavos tarpine kasa',
'partner_code': False,
'start_balance': 0.0,
'end_credit': 0.0,
'start_credit': 0.0,
'end_debit': 100.0,
'start_debit': 0.0,
'end_balance': 100.0
}
},
'end_balance': 100.0
},
account.account(420,
): {
'period_credit': 100.0,
'code': u'273012',
'period_debit': 100.0,
'name': u'Jonavos tarpin\u0117 kasa',
'start_balance': 0.0,
'end_credit': 0.0,
'start_credit': 0.0,
'end_debit': 0.0,
'start_debit': 0.0,
'partner_dicts': {
res.partner(48341,
): {
'period_credit': 0.0,
'code': u'273012',
'period_debit': 100.0,
'name': u'I\u012e AGAVA IR KO',
'partner_code': False,
'start_balance': 0.0,
'end_credit': 0.0,
'start_credit': 0.0,
'end_debit': 100.0,
'start_debit': 0.0,
'end_balance': 100.0
},
res.partner(225485,
): {
'period_credit': 100.0,
'code': u'273012',
'period_debit': 0.0,
'name': u'jonavos tarpine kasa',
'partner_code': False,
'start_balance': 0.0,
'end_credit': 100.0,
'start_credit': 0.0,
'end_debit': 0.0,
'start_debit': 0.0,
'end_balance': -100.0
}
},
'end_balance': 0.0
},
account.account(124,
): {
'period_credit': 0.0,
'code': u'2010',
'period_debit': 0.4,
'name': u'\u017daliav\u0173, med\u017eiag\u0173 ir komplektavimo detali\u0173 \u012fsigijimo savikaina',
'start_balance': 0.0,
'end_credit': 0.0,
'start_credit': 0.0,
'end_debit': 0.4,
'start_debit': 0.0,
'partner_dicts': {
res.partner(205090,
): {
'period_credit': 0.0,
'code': u'2010',
'period_debit': 0.4,
'name': u'PIRK\u0116JAI',
'partner_code': False,
'start_balance': 0.0,
'end_credit': 0.0,
'start_credit': 0.0,
'end_debit': 0.4,
'start_debit': 0.0,
'end_balance': 0.4
}
},
'end_balance': 0.4
},
account.account(300,
): {
'period_credit': 0.4,
'code': u'600000',
'period_debit': 0.0,
'name': u'Parduot\u0173 preki\u0173 savikaina',
'start_balance': 0.0,
'end_credit': 0.4,
'start_credit': 0.0,
'end_debit': 0.0,
'start_debit': 0.0,
'partner_dicts': {
res.partner(205090,
): {
'period_credit': 0.4,
'code': u'600000',
'period_debit': 0.0,
'name': u'PIRK\u0116JAI',
'partner_code': False,
'start_balance': 0.0,
'end_credit': 0.4,
'start_credit': 0.0,
'end_debit': 0.0,
'start_debit': 0.0,
'end_balance': -0.4
}
},
'end_balance': -0.4
},
account.account(281,
): {
'period_credit': 1.18,
'code': u'500000',
'period_debit': 1.18,
'name': u'Parduot\u0173 preki\u0173 pajamos',
'start_balance': 0.0,
'end_credit': 0.0,
'start_credit': 0.0,
'end_debit': 0.0,
'start_debit': 0.0,
'partner_dicts': {
res.partner(205090,
): {
'period_credit': 1.18,
'code': u'500000',
'period_debit': 1.18,
'name': u'PIRK\u0116JAI',
'partner_code': False,
'start_balance': 0.0,
'end_credit': 0.0,
'start_credit': 0.0,
'end_debit': 0.0,
'start_debit': 0.0,
'end_balance': 0.0
}
},
'end_balance': 0.0
},
account.account(156,
): {
'period_credit': 101.43,
'code': u'241000',
'period_debit': 1.43,
'name': u'Pirk\u0117j\u0173 skol\u0173 vert\u0117',
'start_balance': 0.0,
'end_credit': 100.0,
'start_credit': 0.0,
'end_debit': 0.0,
'start_debit': 0.0,
'partner_dicts': {
res.partner(205090,
): {
'period_credit': 1.43,
'code': u'241000',
'period_debit': 1.43,
'name': u'PIRK\u0116JAI',
'partner_code': False,
'start_balance': 0.0,
'end_credit': 0.0,
'start_credit': 0.0,
'end_debit': 0.0,
'start_debit': 0.0,
'end_balance': 0.0
},
res.partner(48341,
): {
'period_credit': 100.0,
'code': u'241000',
'period_debit': 0.0,
'name': u'I\u012e AGAVA IR KO',
'partner_code': False,
'start_balance': 0.0,
'end_credit': 100.0,
'start_credit': 0.0,
'end_debit': 0.0,
'start_debit': 0.0,
'end_balance': -100.0
}
},
'end_balance': -100.0
},
account.account(275,
): {
'period_credit': 0.25,
'code': u'449200',
'period_debit': 0.25,
'name': u'Mok\u0117tinas prid\u0117tin\u0117s vert\u0117s mokestis',
'start_balance': 0.0,
'end_credit': 0.0,
'start_credit': 0.0,
'end_debit': 0.0,
'start_debit': 0.0,
'partner_dicts': {
res.partner(205090,
): {
'period_credit': 0.25,
'code': u'449200',
'period_debit': 0.25,
'name': u'PIRK\u0116JAI',
'partner_code': False,
'start_balance': 0.0,
'end_credit': 0.0,
'start_credit': 0.0,
'end_debit': 0.0,
'start_debit': 0.0,
'end_balance': 0.0
}
},
'end_balance': 0.0
}
}
sorted_account_dicts = sorted(account_dicts, key=lambda x: account_dicts[x].get('name', float('inf')))
sorted_account_dicts = sorted(account_dicts, key=itemgetter('name'))
sorted_account_dicts = sorted(account_dicts.items(), key=lambda x: x[1]['name'], reverse=False)
<t t-foreach="data_vals['account_dicts']" t-as="account">
<t t-set="account_dict" t-value="data_vals['account_dicts'][account]"/>
<tr style="height: 15px; font-size: 6pt; font-weight: bold; text-align: right; vertical-align: top;">
<td style="font-size: 7pt; text-align: left;">
<span t-esc="account['code']"/>
</td>
<td style="font-size: 7pt; text-align: left;">
<span t-esc="account['name']"/>
</td>
... other code...
Answer the question
In order to leave comments, you need to log in
the problem was solved like this:
from collections import OrderedDict
....
sorted_account_dicts = OrderedDict(sorted(account_dicts.items(), key=lambda x: x[1]['name'], reverse=False))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question