# How to sort tuples

**URL:** <https://community.seqera.io/t/how-to-sort-tuples/842>\
**Category:** Ask for help\
**Created:** [June 12, 2024, 8:52pm UTC](https://community.seqera.io/t/how-to-sort-tuples/842 "2024-06-12T20:52:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ramirobarrantes](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/ramirobarrantes/32/717_2.png) [@ramirobarrantes](https://community.seqera.io/u/ramirobarrantes)\
**Post date:** [June 12, 2024, 8:52pm UTC](https://community.seqera.io/t/how-to-sort-tuples/842/1 "2024-06-12T20:52:15Z")

</div>

Hello, I am grouping a set of samples by sample, which I do by the following:

> SAMTOOLS\_MERGE.out.bam.mix(CREATEREADNUM.out.readnum).mix(CLIPPER.out.bed)  
> .groupTuple(by: [0])  
> .map {  
> meta,result →   
> [meta, meta.sample, result[0], result[1], result[2] ]}  
> .groupTuple(by: [1])  
> .map {  
> result →   
> [[id:result[0].sample[0],sample:result[0].sample[0],single\_end:result[0].single\_end[0]],result[2],result[3],result[4]]}  
> .set { ch\_bamreadbed }

and I end up with this, which is what I want:

> [[id:test1, sample:test1, single\_end:false], [test1\_signal.bam,test1\_background.bam], [test1\_signal.readnum.txt, test1\_background.readnum.txt], [test1\_signal.clip.peakClusters.bed, test1\_background.clip.peakClusters.bed]]

However, I would like to **ensure** that a certain order is always preserved: signal always goes before background (or viceversa). What command could help me with that? I could not find a sort and I am not sure how I would apply it here.

---

<div class="post-metadata">

**Author:** ![CarsonJM](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/carsonjm/32/753_2.png) [@CarsonJM](https://community.seqera.io/u/CarsonJM)\
**Post date:** [June 12, 2024, 9:10pm UTC](https://community.seqera.io/t/how-to-sort-tuples/842/2 "2024-06-12T21:10:54Z")

</div>

Hi @ramirobarrantes. I have run into something similar before and think that maybe the `sort` argument of `groupTuple()` might help. Several sorting criteria are included by default, but you can also create custom sorting criteria if necessary.

Here’s a link to the documentation ( [Operators — Nextflow documentation](https://www.nextflow.io/docs/latest/operator.html#grouptuple)) if you scroll down a bit you should be able to see the sorting options.

Please let me know if this is helpful!  
**Carson Miller**

---

<div class="post-metadata">

**Author:** ![mribeirodantas](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/mribeirodantas/32/235_2.png) [@mribeirodantas](https://community.seqera.io/u/mribeirodantas)\
**Post date:** [June 13, 2024, 3:57am UTC](https://community.seqera.io/t/how-to-sort-tuples/842/3 "2024-06-13T03:57:53Z")

</div>

Adding to what @CarsonJM explained, [this Nextflow](https://community.seqera.io/t/group-tuples-based-on-two-meta-map-keys-and-sort-based-on-another-one/130) snippet may help you achieve what you’re looking for 😉

---

<div class="post-metadata">

**Author:** ![CarsonJM](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/carsonjm/32/753_2.png) [@CarsonJM](https://community.seqera.io/u/CarsonJM)\
**Post date:** [June 13, 2024, 2:04pm UTC](https://community.seqera.io/t/how-to-sort-tuples/842/4 "2024-06-13T14:04:58Z")

</div>

@mribeirodantas I will definitely be saving this snipped for future reference 😅

---

<div class="post-metadata">

**Author:** ![ramirobarrantes](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/ramirobarrantes/32/717_2.png) [@ramirobarrantes](https://community.seqera.io/u/ramirobarrantes)\
**Post date:** [June 13, 2024, 2:48pm UTC](https://community.seqera.io/t/how-to-sort-tuples/842/5 "2024-06-13T14:48:05Z")

</div>

THis is excellent, thank you!!!
