Python - How to sort a list, by an attribute from another class

后端 未结 2 641
北海茫月
北海茫月 2020-12-22 07:50

I have some elements in a list in one class. I want them sorted in a new list, and they have to be sorted by an attribute from another class.

Can anyone give an exa

2条回答
  •  旧巷少年郎
    2020-12-22 08:37

    To sort a list of objects that have the attribute bar:

    anewlist = sorted(list, key=lambda x: x.bar)
    

提交回复
热议问题