django - Sorting Python lists of objects by keys with different names -


i used merge , sort 2 lists of objects both have "created" attributes datetime.

i dit way , worked well:

all_events = sorted(     chain(list1, list2),         key=attrgetter('created')) 

now, i'd add 3rd list in sort. problem datetime attribute of objects in 3rd list not named 'created'.

how sort 3 lists in case?

thank you.

use lambda attribute fallback:

all_events = sorted(     chain(list1, list2, list3),     key=lambda o: getattr(o, 'created', getattr(o, 'otherattribute', none)) 

this try either attribute, preferring 'created'.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -