Skip to content

Commit c43871c

Browse files
committed
Add new json_object_array_sort function
- uses libc's qsort to sort the arraylist - add test in test1.c
1 parent a8ffbe9 commit c43871c

File tree

6 files changed

+83
-0
lines changed

6 files changed

+83
-0
lines changed

arraylist.c

+7
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ array_list_add(struct array_list *arr, void *data)
8787
return array_list_put_idx(arr, arr->length, data);
8888
}
8989

90+
void
91+
array_list_sort(struct array_list *arr, int(*sort_fn)(const void *, const void *))
92+
{
93+
qsort(arr->array, arr->length, sizeof(arr->array[0]),
94+
(int (*)(const void *, const void *))sort_fn);
95+
}
96+
9097
int
9198
array_list_length(struct array_list *arr)
9299
{

arraylist.h

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ array_list_add(struct array_list *al, void *data);
4646
extern int
4747
array_list_length(struct array_list *al);
4848

49+
extern void
50+
array_list_sort(struct array_list *arr, int(*compar)(const void *, const void *));
51+
4952
#ifdef __cplusplus
5053
}
5154
#endif

json_object.c

+5
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,11 @@ struct array_list* json_object_get_array(struct json_object *jso)
534534
}
535535
}
536536

537+
void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *, const void *))
538+
{
539+
array_list_sort(jso->o.c_array, sort_fn);
540+
}
541+
537542
int json_object_array_length(struct json_object *jso)
538543
{
539544
return array_list_length(jso->o.c_array);

json_object.h

+10
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,16 @@ extern struct array_list* json_object_get_array(struct json_object *obj);
195195
*/
196196
extern int json_object_array_length(struct json_object *obj);
197197

198+
/** Sorts the elements of jso of type json_type_array
199+
*
200+
* Pointers to the json_object pointers will be passed as the two arguments
201+
* to @sort_fn
202+
*
203+
* @param obj the json_object instance
204+
* @param sort_fn a sorting function
205+
*/
206+
extern void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *, const void *));
207+
198208
/** Add an element to the end of a json_object of type json_type_array
199209
*
200210
* The reference count will *not* be incremented. This is to make adding

test1.c

+44
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,29 @@
66

77
#include "json.h"
88

9+
static int sort_fn (const void *j1, const void *j2)
10+
{
11+
json_object **jso1, **jso2;
12+
int i1, i2;
13+
14+
jso1 = j1;
15+
jso2 = j2;
16+
if (!*jso1 && !*jso2) {
17+
return 0;
18+
}
19+
if (!*jso1) {
20+
return -1;
21+
}
22+
if (!*jso2) {
23+
return 1;
24+
}
25+
26+
i1 = json_object_get_int(*jso1);
27+
i2 = json_object_get_int(*jso2);
28+
29+
return i1 - i2;
30+
}
31+
932
int main(int argc, char **argv)
1033
{
1134
json_tokener *tok;
@@ -45,6 +68,27 @@ int main(int argc, char **argv)
4568
}
4669
printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
4770

71+
json_object_put(my_array);
72+
73+
my_array = json_object_new_array();
74+
json_object_array_add(my_array, json_object_new_int(3));
75+
json_object_array_add(my_array, json_object_new_int(1));
76+
json_object_array_add(my_array, json_object_new_int(2));
77+
json_object_array_put_idx(my_array, 4, json_object_new_int(0));
78+
printf("my_array=\n");
79+
for(i=0; i < json_object_array_length(my_array); i++) {
80+
json_object *obj = json_object_array_get_idx(my_array, i);
81+
printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
82+
}
83+
printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
84+
json_object_array_sort(my_array, sort_fn);
85+
printf("my_array=\n");
86+
for(i=0; i < json_object_array_length(my_array); i++) {
87+
json_object *obj = json_object_array_get_idx(my_array, i);
88+
printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
89+
}
90+
printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
91+
4892
my_object = json_object_new_object();
4993
json_object_object_add(my_object, "abc", json_object_new_int(12));
5094
json_object_object_add(my_object, "foo", json_object_new_string("bar"));

test1.expected

+14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ my_array=
1313
[3]=null
1414
[4]=5
1515
my_array.to_string()=[ 1, 2, 3, null, 5 ]
16+
my_array=
17+
[0]=3
18+
[1]=1
19+
[2]=2
20+
[3]=null
21+
[4]=0
22+
my_array.to_string()=[ 3, 1, 2, null, 0 ]
23+
my_array=
24+
[0]=null
25+
[1]=0
26+
[2]=1
27+
[3]=2
28+
[4]=3
29+
my_array.to_string()=[ null, 0, 1, 2, 3 ]
1630
my_object=
1731
abc: 12
1832
foo: "bar"

0 commit comments

Comments
 (0)