# Help with XPDO getIterator complex sorting

**URL:** <https://community.modx.com/t/help-with-xpdo-getiterator-complex-sorting/6671>\
**Category:** Support\
**Created:** [April 27, 2023, 3:18pm UTC](https://community.modx.com/t/help-with-xpdo-getiterator-complex-sorting/6671 "2023-04-27T15:18:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![robcarey](https://community.modx.com/user_avatar/community.modx.com/robcarey/32/688_2.png) [@robcarey](https://community.modx.com/u/robcarey)\
**Post date:** [April 27, 2023, 3:18pm UTC](https://community.modx.com/t/help-with-xpdo-getiterator-complex-sorting/6671/1 "2023-04-27T15:18:06Z")

</div>

Hi All!

I am trying to do a somewhat complex sort using xpdo / getIterator.

I need to be able to sort by the field ‘dam\_id’ as compared to a comma delimited list of the ids.

Essentially it is this method/example:

```auto
SELECT id, name
FROM mytable 
WHERE id IN (77, 3, 123, 1) 
ORDER BY FIELD(id, 77, 3, 123, 1)

```

as explained here: [php - MySQL sort by some list - Stack Overflow](https://stackoverflow.com/questions/6810793/mysql-sort-by-some-list)

my code so far (no sorting at all currently)

```auto
$galleryCollection = $modx->getIterator(classDAMResources, ['filename:!=' => '', 'dam_id:IN' => $fullIDList]);
foreach ($galleryCollection as $galleryItem) {
   $galleryItems = [
       'filename' => $galleryItem->get('filename'),
       'dam_id' => $galleryItem->get('dam_id'),
       'caption' => $galleryItem->get('caption')
   ];
   
   $output .= $modx->getChunk('chunk.inspirationgallery.item.tpl', $galleryItems);
}

```

I am assuming I need some line similar to the below before my foreach:

```auto
$galleryCollection->sortby(???);

```

any guidance is appreciated!

---

<div class="post-metadata">

**Author:** ![opengeek](https://community.modx.com/user_avatar/community.modx.com/opengeek/32/1847_2.png) [@opengeek](https://community.modx.com/u/opengeek)\
**Post date:** [April 27, 2023, 3:34pm UTC](https://community.modx.com/t/help-with-xpdo-getiterator-complex-sorting/6671/2 "2023-04-27T15:34:53Z")

</div>

Something like this is what you want…

> [@robcarey](#):
>
> ```auto
> $galleryCollectionQuery = $modx->newQuery(classDAMResources, ['filename:!=' => '', 'dam_id:IN' => $fullIDList]);
> $galleryCollectionQuery->sortby('FIELD(dam_id, ' . implode(',', $fullIDList) . ')');
> $galleryCollection = $modx->getIterator(classDAMResources, $galleryCollectionQuery);
> foreach ($galleryCollection as $galleryItem) {
> $galleryItems = [
> 'filename' => $galleryItem->get('filename'),
> 'dam_id' => $galleryItem->get('dam_id'),
> 'caption' => $galleryItem->get('caption')
> ];
>    
> $output .= $modx->getChunk('chunk.inspirationgallery.item.tpl', $galleryItems);
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![robcarey](https://community.modx.com/user_avatar/community.modx.com/robcarey/32/688_2.png) [@robcarey](https://community.modx.com/u/robcarey)\
**Post date:** [April 27, 2023, 3:46pm UTC](https://community.modx.com/t/help-with-xpdo-getiterator-complex-sorting/6671/3 "2023-04-27T15:46:19Z")

</div>

That is EXACTLY like what I want. 🙂

Thanks @opengeek !

---

<div class="post-metadata">

**Author:** ![system](https://community.modx.com/user_avatar/community.modx.com/system/32/2_2.png) [@system](https://community.modx.com/u/system)\
**Post date:** [April 29, 2023, 3:46pm UTC](https://community.modx.com/t/help-with-xpdo-getiterator-complex-sorting/6671/4 "2023-04-29T15:46:26Z")

</div>

Closed on April 29, 2023, 3:46pm UTC

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.
