Sorting ObjectCollections

I’d really like to sort an ObjectCollection full of TrackObjects by album, and then by track number. Is there a built in method for that or do I need to work some Python magic?

You need to work Python magic - there’s no built-in method because it’s pretty easy to do using standard functions. The objects in the ObjectContainer are available as a list via the “objects” property, and can be sorted using a lambda function:



<br />
oc.objects.sort(key = lambda obj: obj.title)<br />



So I did find that in a different thread, but upon first glance I didn't think it would allow me to sort by multiple keys and I dismissed it. After you posted that I tried to do it blindly with a tuple as my keys and it actually worked.

<br />
oc.objects.sort(key = lambda obj: (obj.album, obj.index))<br />



I was sure it was going to fail but it worked beautifully. I love it when that happens.


That happens with python a lot I find!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.