There is a list of objects:

[<imagemanager.util.DirImageItem object at 0xaa1bf8c>, <imagemanager.util.DirImageItem object at 0xa8f984c>, ...] 

If you look at the insides of one of the objects items[0].__dict__ , then this is what we will see:

 { 'fields': { 'attribs': 'RW', 'modifed': '2007-05-15 11:54', 'name': 'kl9e2itd07xcayohm5rq.png', 'created': '2010-02-22 13:36', 'url': '/media/news/kl9e2itd07xcayohm5rq.png', 'custom': {'width': 100, 'editable': False, 'thumbnail': True, 'height': 100}, 'path': '/media/news/kl9e2itd07xcayohm5rq.png', 'type': 'png', 'size': 435 }, 'filename': <dUrl /media/news/kl9e2itd07xcayohm5rq.png>} 

This contains each object, they need to be sorted by date created . Sort by new to old.

Tried like this:

 items = sorted(items, key=lambda k: k.fields['modifed']) 

Alas, nothing has changed.

Tell me what I'm doing wrong, or another way to sort the objects.

    2 answers 2

    You should use:

     items = sorted(items, key=lambda x: x['fields']['modifed']) или items.sort(key=lambda x: x['fields']['modifed']) 

    Similarly, for created, replace 'modifed'.

    • Not so does not roll. So I tried, an error. Just as I wrote if to apply, then you can get the data, in this case we get "2007-05-15 11:54" - trec
    • provide the text of the error - Kozlov Sergei
    • 'DirImageItem' object is unsubscriptable - trec
    • can then be obtained through the dict field? : x .__ dict __ ['fields'] ['modifed'] - Kozlov Sergei
    • So there is no error, but nothing happens to the files, everything is in the same order. Tell me, in general, is this scheme correct? It is possible that they are simply sorted at that stage, but further on, it makes no sense at output. Tomorrow I will examine at each stage whether objects are sorted. - trec

    This contains each object, they need to be sorted by date created .

    items = sorted (items, key = lambda k: k.fields [' modifed '])

    So by created or by modified ?

    Also make sure that items[n].k.fields['created'] for all items returns exactly what you expect. Otherwise, everything is correct.

    • Led all the data, I realized that they are the same. Above wrote why. And so any of the options was true. Thank. - trec