10 static void *get_next(const void *a
)
12 return ((const struct line
*)a
)->next
;
15 static void set_next(void *a
, void *b
)
17 ((struct line
*)a
)->next
= b
;
20 static int compare_strings(const void *a
, const void *b
)
22 const struct line
*x
= a
, *y
= b
;
23 return strcmp(x
->text
, y
->text
);
26 int cmd__mergesort(int argc
, const char **argv
)
28 struct line
*line
, *p
= NULL
, *lines
= NULL
;
29 struct strbuf sb
= STRBUF_INIT
;
32 if (strbuf_getwholeline(&sb
, stdin
, '\n'))
34 line
= xmalloc(sizeof(struct line
));
35 line
->text
= strbuf_detach(&sb
, NULL
);
46 lines
= llist_mergesort(lines
, get_next
, set_next
, compare_strings
);
49 printf("%s", lines
->text
);