MCPcopy Create free account
hub / github.com/DaveGamble/cJSON / sort_list

Function sort_list

cJSON_Utils.c:484–593  ·  view source on GitHub ↗

sort lists using mergesort */

Source from the content-addressed store, hash-verified

482
483/* sort lists using mergesort */
484static cJSON *sort_list(cJSON *list, const cJSON_bool case_sensitive)
485{
486 cJSON *first = list;
487 cJSON *second = list;
488 cJSON *current_item = list;
489 cJSON *result = list;
490 cJSON *result_tail = NULL;
491
492 if ((list == NULL) || (list->next == NULL))
493 {
494 /* One entry is sorted already. */
495 return result;
496 }
497
498 while ((current_item != NULL) && (current_item->next != NULL) && (compare_strings((unsigned char*)current_item->string, (unsigned char*)current_item->next->string, case_sensitive) < 0))
499 {
500 /* Test for list sorted. */
501 current_item = current_item->next;
502 }
503 if ((current_item == NULL) || (current_item->next == NULL))
504 {
505 /* Leave sorted lists unmodified. */
506 return result;
507 }
508
509 /* reset pointer to the beginning */
510 current_item = list;
511 while (current_item != NULL)
512 {
513 /* Walk two pointers to find the middle. */
514 second = second->next;
515 current_item = current_item->next;
516 /* advances current_item two steps at a time */
517 if (current_item != NULL)
518 {
519 current_item = current_item->next;
520 }
521 }
522 if ((second != NULL) && (second->prev != NULL))
523 {
524 /* Split the lists */
525 second->prev->next = NULL;
526 second->prev = NULL;
527 }
528
529 /* Recursively sort the sub-lists. */
530 first = sort_list(first, case_sensitive);
531 second = sort_list(second, case_sensitive);
532 result = NULL;
533
534 /* Merge the sub-lists */
535 while ((first != NULL) && (second != NULL))
536 {
537 cJSON *smaller = NULL;
538 if (compare_strings((unsigned char*)first->string, (unsigned char*)second->string, case_sensitive) < 0)
539 {
540 smaller = first;
541 }

Callers 1

sort_objectFunction · 0.85

Calls 1

compare_stringsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…