diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4c95cb23..d0a7ab1e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -141,6 +141,7 @@
* Added libgen.h for baseline()
### Corrected Bugs
+* Fixed a problem caused by recent sorting patches that made "ordered-by user" lists fail in some cases, causing multiple list entries with same keys. NACM being one example. Thanks vratnikov!
* [Restconf does not handle startup datastore according to the RFC](https://github.com/clicon/clixon/issues/74)
* Failure in startup with -m startup or running left running_db cleared.
* Running-db should not be changed on failure. Unless failure-db defined. Or if SEGV, etc. In those cases, tmp_db should include the original running-db.
diff --git a/lib/src/clixon_xml_sort.c b/lib/src/clixon_xml_sort.c
index fb063250..e3cef110 100644
--- a/lib/src/clixon_xml_sort.c
+++ b/lib/src/clixon_xml_sort.c
@@ -416,11 +416,9 @@ xml_search1(cxobj *xp,
cmp = yangi-yang_order(y);
/* Here is right yang order == same yang? */
if (cmp == 0){
- if (userorder){
+ cmp = xml_cmp(x1, xc, 0);
+ if (cmp && userorder) /* Ordered by user (if not equal) */
return xml_search_userorder(xp, x1, y, yangi, mid);
- }
- else /* Ordered by system */
- cmp = xml_cmp(x1, xc, 0);
}
if (cmp == 0)
return xc;
diff --git a/test/test_order.sh b/test/test_order.sh
index 82171613..4ac5bec3 100755
--- a/test/test_order.sh
+++ b/test/test_order.sh
@@ -217,6 +217,24 @@ expecteof "$clixon_netconf -qf $cfg -y $fyang" 0 ']]>]]>' '^cbarbfooafie]]>]]>$'
+new "Overwrite existing ordered-by user y2->c"
+expecteof "$clixon_netconf -qf $cfg -y $fyang" 0 '
+cnewc
+]]>]]>'
+
+new "Overwrite existing ordered-by user y2->b"
+expecteof "$clixon_netconf -qf $cfg -y $fyang" 0 '
+bnewb
+]]>]]>'
+
+new "Overwrite existing ordered-by user y2->a"
+expecteof "$clixon_netconf -qf $cfg -y $fyang" 0 '
+anewa
+]]>]]>'
+
+new "Tests for no duplicates."
+expecteof "$clixon_netconf -qf $cfg -y $fyang" 0 ']]>]]>' '^cnewcbnewbanewa]]>]]>$'
+
#-- order by type rather than strings.
# there are three leaf-lists:strings, ints, and decimal64, and two lists:
# listints and listdecs