diff --git a/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuPatPrimitives/CMakeLists.txt b/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuPatPrimitives/CMakeLists.txt
index 3347cb848e848ce8c18dbcfaf069c62d2cfb6ac8..c74632ff1bbb6d02c849a18addfd2c658431a859 100644
--- a/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuPatPrimitives/CMakeLists.txt
+++ b/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuPatPrimitives/CMakeLists.txt
@@ -11,7 +11,7 @@ atlas_add_library( MuPatPrimitives
                    src/*.cxx
                    PUBLIC_HEADERS MuPatPrimitives
                    LINK_LIBRARIES CxxUtils EventPrimitives TrkMeasurementBase TrkParameters MuonStationIndexLib 
-			          MuonSegment TrkSegment TrkTrack TrkTrackSummary)
+			          MuonSegment TrkSegment TrkTrack TrkTrackSummary MuonIdHelpersLib)
 
 # Install files from the package:
 #atlas_install_joboptions( share/*.py )
diff --git a/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuPatPrimitives/MuPatPrimitives/SortMuPatHits.h b/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuPatPrimitives/MuPatPrimitives/SortMuPatHits.h
index d0a70c3bb424c3da653a00bd09fafc3b447e68a2..c3b99d8173ed702d8f741c8305ee773dc3acdc43 100644
--- a/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuPatPrimitives/MuPatPrimitives/SortMuPatHits.h
+++ b/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuPatPrimitives/MuPatPrimitives/SortMuPatHits.h
@@ -9,6 +9,7 @@
 #include "TrkMeasurementBase/MeasurementBase.h"
 #include "TrkParameters/TrackParameters.h"
 #include "TrkSurfaces/Surface.h"
+#include "MuonIdHelpers/IMuonIdHelperSvc.h"
 
 namespace Muon {
 
@@ -39,7 +40,38 @@ namespace Muon {
 
     class SortMuPatHits {
     public:
-        bool operator()(const MuPatHit* hit1, const MuPatHit* hit2) const {
+      bool operator()(const MuPatHit* hit1, const MuPatHit* hit2, const IMuonIdHelperSvc* idh) const {
+	    //first, check if both hits are in the same chamber, and at least one is an RPC
+	    //if so, do some ID-based sorting
+	    if(idh->chamberIndex(hit1->info().id) == idh->chamberIndex(hit2->info().id) && (hit1->info().type==1 || hit2->info().type==1)){
+	        if(hit1->info().type==0){
+		    if (idh->rpcIdHelper().doubletR(hit2->info().id) == 1) {
+		        if (idh->stationIndex(hit1->info().id) == MuonStationIndex::StIndex::BM || idh->isSmallChamber(hit1->info().id))
+		            return true;
+		        else return false;
+		    } 
+		    else return false;
+                }
+		else if(hit2->info().type==0){
+		    if (idh->rpcIdHelper().doubletR(hit1->info().id) == 1) {
+		        if (idh->stationIndex(hit2->info().id) == MuonStationIndex::StIndex::BM || idh->isSmallChamber(hit2->info().id))
+			    return false;
+			else return true;
+		    }
+		    else return true;
+		}
+		else{ //both hits are RPC
+		    Identifier id1=hit1->info().id;
+		    Identifier id2=hit2->info().id;
+		    if (idh->rpcIdHelper().doubletR(id1) != idh->rpcIdHelper().doubletR(id2)) {
+		        return idh->rpcIdHelper().doubletR(id1) > idh->rpcIdHelper().doubletR(id2);
+		    } else if (idh->rpcIdHelper().doubletZ(id1) != idh->rpcIdHelper().doubletZ(id2)) {
+		        return idh->rpcIdHelper().doubletZ(id1) > idh->rpcIdHelper().doubletZ(id2);
+		    } else if (idh->rpcIdHelper().doubletPhi(id1) != idh->rpcIdHelper().doubletPhi(id2)) {
+		        return idh->rpcIdHelper().doubletPhi(id1) > idh->rpcIdHelper().doubletPhi(id2);
+		    } //last case can be handled below
+		}
+	    }
             double dist = distanceCalculator(hit1->parameters(), hit2->parameters());
             if (dist < -0.01)
                 return true;
diff --git a/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuonTrackSteeringTools/src/MuPatHitTool.cxx b/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuonTrackSteeringTools/src/MuPatHitTool.cxx
index eecaad1371d4b427aeb316f71ce95ee51e9bf966..8ee1c0b7e02ad0bb8d07479fef2b80a67baa3b90 100644
--- a/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuonTrackSteeringTools/src/MuPatHitTool.cxx
+++ b/MuonSpectrometer/MuonReconstruction/MuonTrackMakers/MuonTrackMakerTools/MuonTrackSteeringTools/src/MuPatHitTool.cxx
@@ -404,7 +404,7 @@ namespace Muon {
         if (pos == list.end()) { --pos; }
 
         SortMuPatHits isLargerCal{};
-        bool isLarger = isLargerCal(hit, *pos);  // check whether the hit is larger that the current list item
+        bool isLarger = isLargerCal(hit, *pos, &*m_idHelperSvc);  // check whether the hit is larger that the current list item
         bool isLargerInit = isLarger;            // to know which direction we moved
 
         // check whether the hit is larger that the current list item
@@ -415,7 +415,7 @@ namespace Muon {
                 // if we reached the end of the list, insert the hit at the end
                 if (pos == list.end()) {
                     // check whether hit duplicate of last hit in list
-                    if (isLargerCal(list.back(), hit) != isLargerCal(hit, list.back()) ||
+		    if (isLargerCal(list.back(), hit, &*m_idHelperSvc) != isLargerCal(hit, list.back(), &*m_idHelperSvc) ||
                         (hit->info().type == MuPatHit::MM && hit->info().id != list.back()->info().id)) {
                         ATH_MSG_VERBOSE(" inserting hit at back   " << m_idHelperSvc->toString(hit->info().id) << " "
                                                                     << m_printer->print(hit->parameters()));
@@ -429,18 +429,18 @@ namespace Muon {
                     }
                     return --pos;
                 }
-                isLarger = isLargerCal(hit, *pos);  // recalculate distance
+                isLarger = isLargerCal(hit, *pos, &*m_idHelperSvc);  // recalculate distance
             }
         } else {
             // as long as the hit is smaller and we didn't reach the beginning of the list take a step back
             while (pos != list.begin() && !isLarger) {
                 --pos;                              // take a step back
-                isLarger = isLargerCal(hit, *pos);  // recalculate distance
+                isLarger = isLargerCal(hit, *pos, &*m_idHelperSvc);  // recalculate distance
             }
             // if we reached the first list item, check whether current hit is smaller. If so insert before first.
             if (pos == list.begin() && !isLarger) {
                 // check whether hit duplicate of last hit in list
-                if (isLargerCal(list.front(), hit) != isLargerCal(hit, list.front()) ||
+	        if (isLargerCal(list.front(), hit, &*m_idHelperSvc) != isLargerCal(hit, list.front(), &*m_idHelperSvc) ||
                     (hit->info().type == MuPatHit::MM && hit->info().id != list.front()->info().id)) {
                     ATH_MSG_VERBOSE(" inserting hit at front  " << m_idHelperSvc->toString(hit->info().id) << " "
                                                                 << m_printer->print(hit->parameters()));
@@ -460,7 +460,7 @@ namespace Muon {
             ++pos;
             if (pos == list.end()) {
                 // check whether hit duplicate of last hit in list
-                if (isLargerCal(list.back(), hit) != isLargerCal(hit, list.back()) ||
+	        if (isLargerCal(list.back(), hit, &*m_idHelperSvc) != isLargerCal(hit, list.back(), &*m_idHelperSvc) ||
                     (hit->info().type == MuPatHit::MM && hit->info().id != list.back()->info().id)) {
                     ATH_MSG_VERBOSE(" inserting hit at back   " << m_idHelperSvc->toString(hit->info().id) << " "
                                                                 << m_printer->print(hit->parameters()));
@@ -473,7 +473,7 @@ namespace Muon {
                 }
                 return --pos;
             }
-            isLarger = isLargerCal(hit, *pos);  // recalculate distance
+            isLarger = isLargerCal(hit, *pos, &*m_idHelperSvc);  // recalculate distance
         }
         // check for chamber sorting issues
         if (m_idHelperSvc->chamberIndex(hit->info().id) != m_idHelperSvc->chamberIndex((*pos)->info().id) && pos != list.begin()) {
@@ -510,7 +510,7 @@ namespace Muon {
         // remove duplicates
 
         // check whether hit and entry at pos are a duplicate
-        if (isLarger == isLargerCal(*pos, hit) && (hit->info().type != MuPatHit::MM || hit->info().id == (*pos)->info().id)) {
+        if (isLarger == isLargerCal(*pos, hit, &*m_idHelperSvc) && (hit->info().type != MuPatHit::MM || hit->info().id == (*pos)->info().id)) {
             // hit is a duplicate
             ATH_MSG_VERBOSE(" NOT inserting duplicate hit  " << m_idHelperSvc->toString(hit->info().id) << " "
                                                              << m_printer->print(hit->parameters()));
@@ -522,7 +522,7 @@ namespace Muon {
             --pos;  // move to previous hit
 
             // check whether hit and entry at pos are a duplicate
-            if (isLargerCal(hit, *pos) == isLargerCal(*pos, hit) &&
+            if (isLargerCal(hit, *pos, &*m_idHelperSvc) == isLargerCal(*pos, hit, &*m_idHelperSvc) &&
                 (hit->info().type != MuPatHit::MM || hit->info().id == (*pos)->info().id)) {
                 ++pos;  // move forward to insert position for pos
                 // hit is a duplicate
@@ -610,10 +610,10 @@ namespace Muon {
             dataOss << "  " << result << " dist " << distance;
             dataStrings.push_back(dataOss.str());
             if (itNext != it_end) {
-                isLarger = isLargerCal(*itNext, *it);
+	        isLarger = isLargerCal(*itNext, *it, &*m_idHelperSvc);
                 distance = distCal(*it, *itNext);
                 result = isLarger ? "larger " : "smaller";
-                if (isLarger == isLargerCal(*it, *itNext)) {
+                if (isLarger == isLargerCal(*it, *itNext, &*m_idHelperSvc)) {
                     result = "duplicate";
                 } else if (!isLarger) {
                     result += "   sorting problem ";
@@ -718,8 +718,8 @@ namespace Muon {
         if (itNext != it_end) ++itNext;
         bool isLarger = true;
         for (; itNext != it_end; ++it, ++itNext) {
-            isLarger = isLargerCal(*it, *itNext);
-            bool sameSurface = (isLarger == isLargerCal(*it, *itNext));  // same surface
+	    isLarger = isLargerCal(*it, *itNext, &*m_idHelperSvc);
+            bool sameSurface = (isLarger == isLargerCal(*it, *itNext, &*m_idHelperSvc));  // same surface
             if (!isLarger && !sameSurface) return false;
             if (sameSurface) return false;
         }