# Sorting ObjectCollections

**URL:** https://forums.plex.tv/t/sorting-objectcollections/21572
**Category:** Dev/API Corner
**Tags:** plugin-dev
**Created:** [September 20, 2012, 5:00am UTC](https://forums.plex.tv/t/sorting-objectcollections/21572 "2012-09-20T05:00:31Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![maik](https://avatars.discourse-cdn.com/v4/letter/m/7feea3/32.png) [@maik](https://forums.plex.tv/u/maik)
#### Post date: [September 20, 2012, 5:00am UTC](https://forums.plex.tv/t/sorting-objectcollections/21572/1 "2012-09-20T05:00:31Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![jam](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/jam/32/186050_2.png) [@jam](https://forums.plex.tv/u/jam)
#### Post date: [September 20, 2012, 3:12pm UTC](https://forums.plex.tv/t/sorting-objectcollections/21572/2 "2012-09-20T15:12:18Z")

</div>

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:

```auto

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

```

---

<div class="post-metadata">

### Author: ![maik](https://avatars.discourse-cdn.com/v4/letter/m/7feea3/32.png) [@maik](https://forums.plex.tv/u/maik)
#### Post date: [September 20, 2012, 9:21pm UTC](https://forums.plex.tv/t/sorting-objectcollections/21572/3 "2012-09-20T21:21:22Z")

</div>

> [@](#):
>
> 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:  
>   
> 
> ```auto
> 
> <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.  

```auto

<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.

---

<div class="post-metadata">

### Author: ![Gerk](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/gerk/32/81980_2.png) [@Gerk](https://forums.plex.tv/u/Gerk)
#### Post date: [September 22, 2012, 2:38pm UTC](https://forums.plex.tv/t/sorting-objectcollections/21572/4 "2012-09-22T14:38:48Z")

</div>

> [@](#):
>
> 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.  
> 
> ```auto
> 
> <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!

---

<div class="post-metadata">

### Author: ![system](https://global.discourse-cdn.com/plex/original/3X/2/a/2acb9765406f63293d357b4ec509ec39aa28f2ad.png) [@system](https://forums.plex.tv/u/system)
#### Post date: [December 20, 2019, 10:16pm UTC](https://forums.plex.tv/t/sorting-objectcollections/21572/5 "2019-12-20T22:16:17Z")

</div>

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