diff --git a/gentrap/src/main/scala/nl/lumc/sasc/biopet/pipelines/gentrap/measures/FragmentsPerGene.scala b/gentrap/src/main/scala/nl/lumc/sasc/biopet/pipelines/gentrap/measures/FragmentsPerGene.scala
index 40cf4ca6798bc5106a81cd92687bef2d49a2f5e4..f06a3b8e857e3b1766d92554c954a6276d880dcb 100644
--- a/gentrap/src/main/scala/nl/lumc/sasc/biopet/pipelines/gentrap/measures/FragmentsPerGene.scala
+++ b/gentrap/src/main/scala/nl/lumc/sasc/biopet/pipelines/gentrap/measures/FragmentsPerGene.scala
@@ -16,6 +16,7 @@ package nl.lumc.sasc.biopet.pipelines.gentrap.measures
 
 import nl.lumc.sasc.biopet.core.annotations.AnnotationGtf
 import nl.lumc.sasc.biopet.extensions.HtseqCount
+import nl.lumc.sasc.biopet.extensions.picard.SortSam
 import nl.lumc.sasc.biopet.utils.config.Configurable
 import org.broadinstitute.gatk.queue.QScript
 
@@ -25,19 +26,31 @@ import org.broadinstitute.gatk.queue.QScript
 class FragmentsPerGene(val root: Configurable) extends QScript with Measurement with AnnotationGtf {
   def mergeArgs = MergeArgs(idCols = List(1), valCol = 2, numHeaderLines = 0, fallback = "0")
 
-  override def fixedValues: Map[String, Any] = Map("htseqcount" -> Map("order" -> "pos"))
+  override def fixedValues: Map[String, Any] = Map("htseqcount" -> Map("order" -> ""))
+
+  lazy val sortOnId: Boolean = config("sort_on_id", default = true)
 
   /** Pipeline itself */
   def biopetScript(): Unit = {
     val jobs = bamFiles.map {
       case (id, file) =>
-        //TODO: ID sorting job
+
+        val bamFile = if (sortOnId) {
+          val sortSam = new SortSam(this)
+          sortSam.input = file
+          sortSam.output = swapExt(outputDir, file, ".bam", ".idsorted.bam")
+          sortSam.sortOrder = "queryname"
+          sortSam.isIntermediate = true
+          add(sortSam)
+          sortSam.output
+        } else file
 
         val job = new HtseqCount(this)
         job.inputAnnotation = annotationGtf
-        job.inputAlignment = file
+        job.inputAlignment = bamFile
         job.output = new File(outputDir, s"$id.$name.counts")
         job.format = Option("bam")
+        job.order = if (sortOnId) Some("name") else Some("pos")
         add(job)
         id -> job
     }