Skip to content

Commit

Permalink
Better sort lists with numerical keys
Browse files Browse the repository at this point in the history
  • Loading branch information
carlgsmith committed Aug 5, 2024
1 parent 4faceb6 commit 09ed81e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
8 changes: 7 additions & 1 deletion models/test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,16 @@
<VALUE name="false" value="false"/>
</NODE>
<NODE name="groups" help="This is a leaf list of integers">
<NODE name="*" mode="rw" help="Member of group" range="1..9"/>
<NODE name="*" mode="rw" help="Member of group" range="1..999"/>
</NODE>
</NODE>
</NODE>
<NODE name="rules" help="This is a list of rules with numerical indexes">
<NODE name="*" help="The rule entry with key index">
<NODE name="index" mode="rw" help="integer" range="1..999"/>
<NODE name="name" mode="rw" help="This is the name of the rule"/>
</NODE>
</NODE>
</NODE>
<NODE name="state" help="State">
<NODE name="counter" mode="r" default="0" help="uint32" range="0..4294967295"/>
Expand Down
19 changes: 17 additions & 2 deletions schema.c
Original file line number Diff line number Diff line change
Expand Up @@ -3392,6 +3392,13 @@ sch_traverse_tree (sch_instance * instance, sch_node * schema, GNode * node, int
return rc;
}

static int _sch_strcmp_ll (const char *stra, const char *strb)
{
int a = g_ascii_strtoll (stra, NULL, 10);
int b = g_ascii_strtoll (strb, NULL, 10);
return a - b;
}

static json_t *
_sch_gnode_to_json (sch_instance * instance, sch_node * schema, xmlNs *ns, GNode * node, int flags, int depth)
{
Expand Down Expand Up @@ -3488,8 +3495,12 @@ _sch_gnode_to_json (sch_instance * instance, sch_node * schema, xmlNs *ns, GNode

if (sch_is_leaf_list (schema) && (flags & SCH_F_JSON_ARRAYS))
{
sch_node *kschema = sch_node_child_first (schema);
if (kschema && xmlHasProp ((xmlNode *)kschema, (const xmlChar *)"range"))
apteryx_sort_children (node, _sch_strcmp_ll);
else
apteryx_sort_children (node, g_strcmp0);
data = json_array ();
apteryx_sort_children (node, g_strcmp0);

DEBUG (flags, "%*s%s[", depth * 2, " ", APTERYX_NAME (node));
for (GNode * child = node->children; child; child = child->next)
Expand All @@ -3515,8 +3526,12 @@ _sch_gnode_to_json (sch_instance * instance, sch_node * schema, xmlNs *ns, GNode
}
else if (sch_is_list (schema) && (flags & SCH_F_JSON_ARRAYS))
{
sch_node *kschema = sch_node_child_first (sch_node_child_first(schema));
if (kschema && xmlHasProp ((xmlNode *)kschema, (const xmlChar *)"range"))
apteryx_sort_children (node, _sch_strcmp_ll);
else
apteryx_sort_children (node, g_strcmp0);
data = json_array ();
apteryx_sort_children (node, g_strcmp0);
for (GNode * child = node->children; child; child = child->next)
{
DEBUG (flags, "%*s%s[%s]\n", depth * 2, " ", APTERYX_NAME (node),
Expand Down

0 comments on commit 09ed81e

Please sign in to comment.