diff --git a/mercurial/dirstateutils/v2.py b/mercurial/dirstateutils/v2.py
index e255a5355971b18c24911aa5aaa74075ee084e09_bWVyY3VyaWFsL2RpcnN0YXRldXRpbHMvdjIucHk=..fc719967efd0be42bc8771baf196035b7b1a1aff_bWVyY3VyaWFsL2RpcnN0YXRldXRpbHMvdjIucHk= 100644
--- a/mercurial/dirstateutils/v2.py
+++ b/mercurial/dirstateutils/v2.py
@@ -272,7 +272,7 @@
         )
         return data, tree_metadata
 
-    sorted_map = sorted(map.items(), key=lambda x: x[0])
+    sorted_map = sorted(map.items(), key=lambda x: x[0].split(b"/"))
 
     # Use a stack to have to only remember the nodes we currently need
     # instead of building the entire tree in memory
diff --git a/tests/test-dirstate.t b/tests/test-dirstate.t
index e255a5355971b18c24911aa5aaa74075ee084e09_dGVzdHMvdGVzdC1kaXJzdGF0ZS50..fc719967efd0be42bc8771baf196035b7b1a1aff_dGVzdHMvdGVzdC1kaXJzdGF0ZS50 100644
--- a/tests/test-dirstate.t
+++ b/tests/test-dirstate.t
@@ -257,5 +257,4 @@
   $ touch src/dirstate/file1 src/dirstate/file2 src/dirstate.rs
   $ touch file1 file2
   $ hg commit -Aqm1
-#if rhg no-rust dirstate-v2
   $ hg st
@@ -261,11 +260,2 @@
   $ hg st
-  ! src/dirstate/file1 (known-bad-output !)
-  ! src/dirstate/file2 (known-bad-output !)
-  ? src/dirstate/file1 (known-bad-output !)
-  ? src/dirstate/file2 (known-bad-output !)
-  expected a value, found none (known-bad-output !)
-  [255]
-#else
-  $ hg st
-#endif
   $ cd ..