plugins types FEATURE sort callback in union
diff --git a/src/plugins_types.h b/src/plugins_types.h
index 09a3b52..25575eb 100644
--- a/src/plugins_types.h
+++ b/src/plugins_types.h
@@ -1122,6 +1122,12 @@
         const struct lyd_value *val2);
 
 /**
+ * @brief Implementation of ::lyplg_type_sort_clb for the built-in union type.
+ */
+LIBYANG_API_DECL int lyplg_type_sort_union(const struct ly_ctx *ctx, const struct lyd_value *val1,
+        const struct lyd_value *val2);
+
+/**
  * @brief Implementation of ::lyplg_type_print_clb for the built-in union type.
  */
 LIBYANG_API_DECL const void *lyplg_type_print_union(const struct ly_ctx *ctx, const struct lyd_value *value,
diff --git a/src/plugins_types/union.c b/src/plugins_types/union.c
index eae2348..32c682c 100644
--- a/src/plugins_types/union.c
+++ b/src/plugins_types/union.c
@@ -429,6 +429,34 @@
     return val1->subvalue->value.realtype->plugin->compare(ctx, &val1->subvalue->value, &val2->subvalue->value);
 }
 
+LIBYANG_API_DEF int
+lyplg_type_sort_union(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
+{
+    int ret;
+    LY_ARRAY_COUNT_TYPE u;
+    struct lysc_type **types;
+
+    if (val1->subvalue->value.realtype == val2->subvalue->value.realtype) {
+        return val1->subvalue->value.realtype->plugin->sort(ctx, &val1->subvalue->value, &val2->subvalue->value);
+    }
+
+    /* compare according to the order of types */
+    ret = 0;
+    types = ((struct lysc_type_union *)val1->realtype)->types;
+    LY_ARRAY_FOR(types, u) {
+        if (types[u] == val1->subvalue->value.realtype) {
+            ret = 1;
+            break;
+        } else if (types[u] == val2->subvalue->value.realtype) {
+            ret = -1;
+            break;
+        }
+    }
+    assert(ret != 0);
+
+    return ret;
+}
+
 /**
  * @brief Create LYB data for printing.
  *
@@ -604,7 +632,7 @@
         .plugin.store = lyplg_type_store_union,
         .plugin.validate = lyplg_type_validate_union,
         .plugin.compare = lyplg_type_compare_union,
-        .plugin.sort = NULL,
+        .plugin.sort = lyplg_type_sort_union,
         .plugin.print = lyplg_type_print_union,
         .plugin.duplicate = lyplg_type_dup_union,
         .plugin.free = lyplg_type_free_union,
diff --git a/tests/utests/types/union.c b/tests/utests/types/union.c
index 07c386e..2c378df 100644
--- a/tests/utests/types/union.c
+++ b/tests/utests/types/union.c
@@ -148,6 +148,45 @@
 }
 
 static void
+test_plugin_sort(void **state)
+{
+    const char *v1, *v2;
+    const char *schema;
+    struct lys_module *mod;
+    struct lyd_value val1 = {0}, val2 = {0};
+    struct lyplg_type *type = lyplg_type_plugin_find("", NULL, ly_data_type2str[LY_TYPE_UNION]);
+    struct lysc_type *lysc_type;
+    struct ly_err_item *err = NULL;
+
+    schema = MODULE_CREATE_YANG("sort", "leaf-list ll {type union {type uint16; type int16;}}");
+    UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
+    lysc_type = ((struct lysc_node_leaflist *)mod->compiled->data)->type;
+
+    v1 = "1";
+    assert_int_equal(LY_SUCCESS, type->store(UTEST_LYCTX, lysc_type, v1, strlen(v1),
+            0, LY_VALUE_JSON, NULL, LYD_VALHINT_DECNUM, NULL, &val1, NULL, &err));
+    v2 = "-1";
+    assert_int_equal(LY_SUCCESS, type->store(UTEST_LYCTX, lysc_type, v2, strlen(v2),
+            0, LY_VALUE_JSON, NULL, LYD_VALHINT_DECNUM, NULL, &val2, NULL, &err));
+    assert_int_equal(1, type->sort(UTEST_LYCTX, &val1, &val2));
+    assert_int_equal(0, type->sort(UTEST_LYCTX, &val1, &val1));
+    assert_int_equal(-1, type->sort(UTEST_LYCTX, &val2, &val1));
+    type->free(UTEST_LYCTX, &val1);
+    type->free(UTEST_LYCTX, &val2);
+
+    v1 = "-1";
+    assert_int_equal(LY_SUCCESS, type->store(UTEST_LYCTX, lysc_type, v1, strlen(v1),
+            0, LY_VALUE_JSON, NULL, LYD_VALHINT_DECNUM, NULL, &val1, NULL, &err));
+    v2 = "-2";
+    assert_int_equal(LY_SUCCESS, type->store(UTEST_LYCTX, lysc_type, v2, strlen(v2),
+            0, LY_VALUE_JSON, NULL, LYD_VALHINT_DECNUM, NULL, &val2, NULL, &err));
+    assert_int_equal(1, type->sort(UTEST_LYCTX, &val1, &val2));
+    assert_int_equal(-1, type->sort(UTEST_LYCTX, &val2, &val1));
+    type->free(UTEST_LYCTX, &val1);
+    type->free(UTEST_LYCTX, &val2);
+}
+
+static void
 test_validation(void **state)
 {
     const char *schema, *data;
@@ -200,6 +239,7 @@
         UTEST(test_data_xml),
         UTEST(test_data_json),
         UTEST(test_plugin_lyb),
+        UTEST(test_plugin_sort),
         UTEST(test_validation),
     };