diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7c1941a8826b5b32ed8ec74f284b175e76027a64..8f01b704efb2b938a85cda8811c44ec06cbf658e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
 * Update conda environment file
 * Update python scripts for python3
 
+### `Fixed`
+
+* Fix a bug in rm_dup in file sorting
+
 ### `Deprecated`
 
 * --skipMaps, --skipIce, --skipCool, --skipMultiQC are deprecated and replaced by --skip_maps, --skip_ice, --skip_cool, --skip_multiqc
diff --git a/main.nf b/main.nf
index 7b6b37452fd4584e5798ee8fd12d4c39dc25e49c..872d6e6994b7207533873ee1bb4ae27d82b3ba20 100644
--- a/main.nf
+++ b/main.nf
@@ -650,8 +650,10 @@ if (!params.dnase){
       if ("$params.min_restriction_fragment_size".isInteger()) opts="${opts} -t ${params.min_restriction_fragment_size}"
       if ("$params.max_restriction_fragment_size".isInteger()) opts="${opts} -m ${params.max_restriction_fragment_size}"
       if (params.save_interaction_bam) opts="${opts} --sam"
+      prefix = pe_bam.toString() - ~/.bam/
       """
       mapped_2hic_fragments.py -f ${frag_file} -r ${pe_bam} --all ${opts}
+      sort -T /tmp/ -k2,2V -k3,3n -k5,5V -k6,6n -o ${prefix}.validPairs ${prefix}.validPairs
       """
    }
 }
@@ -677,8 +679,10 @@ else{
 
       def opts = ""
       if ("$params.min_cis_dist".isInteger()) opts="${opts} -d ${params.min_cis_dist}"
+      prefix = pe_bam.toString() - ~/.bam/
       """
       mapped_2hic_dnase.py -r ${pe_bam} ${opts}
+      sort -T /tmp/ -k2,2V -k3,3n -k5,5V -k6,6n -o ${prefix}.validPairs ${prefix}.validPairs
       """
    }
 }