django-python order json

0

django and I do not know how to sort this list by title order.

I'm using:

nuevo_json_array = sorted(json_array, key=itemgetter('oportunidad_json'))

  [  
   {  
      'pk': 1
      'oportunidad_json':OrderedDict([ 
        ('id',38441),
         ('titulo',   'Barelona'),  ]),
      'fecha':'2018-10-30T02:00:00',
      'clase':'A'
     },
     {  
      'pk': 2
      'oportunidad_json':OrderedDict([ 
        ('id',1223),
         ('titulo',   'Alicante'),  ]),
      'fecha':'2018-10-30T02:00:00',
      'clase':'B'
     },
      {  
      'pk': 3
      'oportunidad_json':OrderedDict([ 
        ('id',577),
         ('titulo',   'Madrid'),  ]),
      'fecha':'2018-10-30T02:00:00',
      'clase':'C'
     }
]

but I get the following error:

unorderable types: collections.OrderedDict() < collections.OrderedDict()
    
asked by Danny Franco 07.11.2018 в 17:41
source

1 answer

0

As I understand in your question, you want to sort the JSON by order (alphabetical) of titulo , which is contained in oportunidad_json . For this you can indicate that your key is titulo by means of a function lambda of the following way:

In [1] > > nuevo_json_array = sorted(json_array, key=lambda t: t['oportunidad_json']['titulo'])

In [2] > > nuevo_json_array

Out [2] > > [{'clase': 'B', 'fecha': '2018-10-30T02:00:00', 'oportunidad_json': OrderedDict([('id', 1223), ('titulo', 'Alicante')]), 'pk': 2}, {'clase': 'A', 'fecha': '2018-10-30T02:00:00', 'oportunidad_json': OrderedDict([('id', 38441), ('titulo', 'Barelona')]), 'pk': 1}, {'clase': 'C', 'fecha': '2018-10-30T02:00:00', 'oportunidad_json': OrderedDict([('id', 577), ('titulo', 'Madrid')]), 'pk': 3}]

    
answered by 08.11.2018 / 13:41
source