I have the following list of lists of "category"
objects in a variable called "category_tree"
, which is a list:
[[<Category: State>, <Category: County>, <Category: Chicago>],
[<Category: Animal>],
[<Category: State>, <Category: County>],
[<Category: State>, <Category: County>, <Category: NYC>],
[<Category: Animal>, <Category: Fish>],
[<Category: State>, <Category: County>, <Category: LA>],
[<Category: Animal>, <Category: Frog>, <Category: TreeFrog>, <Category: Fred>]]
I need to sort the top-level list
by the .name
properties of the underlying lists
. I do not want to sort the underlying lists, just the top-level list
.
Assuming the .name
elements match what is listed here, this is what I'm trying to get:
[[<Category: Animal>],
[<Category: Animal>, <Category: Fish>],
[<Category: Animal>, <Category: Frog>, <Category: TreeFrog>, <Category: Fred>]
[<Category: State>, <Category: County>],
[<Category: State>, <Category: County>, <Category: Chicago>],
[<Category: State>, <Category: County>, <Category: LA>],
[<Category: State>, <Category: County>, <Category: NYC>]]
I can sort them by the first or the last column with:
category_tree.sort(key=lambda x: x[0].name)
category_tree.sort(key=lambda x: x[-1].name)
But the problem I'm running into is that they have variable numbers of columns, so I can't sort them this way for all internal columns.
What is the best way to do this?