+/* for simplicity, the List gains ownership of the line */
+static List *
+list_insert(List *self, Line *line)
+{
+ if (line == NULL) { return NULL; }
+
+ /* first insertion */
+ if (self->head == NULL) {
+ self->head = line;
+ self->current = line;
+
+ /* all subsequent insertions */
+ } else {
+ self->current->next = line;
+ self->current = line;
+ }
+ self->len++;
+ return self;
+}
+
+/* order the list according to compare() */
+static List *
+list_sort(List *self, Compare *compare)
+{
+ int i;
+ Line *line;
+
+ /* mallocate array of Line*s */
+ self->sorted = (Line **) malloc(self->len * sizeof(Line*));
+ if (self->sorted == NULL) { return NULL; }
+
+ /* fill array w/ List's contents */
+ i = 0;
+ line = self->head;
+ while (line) {
+ self->sorted[i++] = line;
+ line = line->next;
+ }
+
+ /* apply qsort */
+ qsort(self->sorted, self->len, sizeof(Line*), compare);
+ return self;
+}
+
+/* precondition: list must be sorted */
+static List *
+list_writeToFile(List *self, FILE* dst)
+{
+ int i;
+ Line **line = self->sorted;
+
+ if (self->sorted == NULL) { return NULL; }
+ for (i = 0; i < self->len; i++) {
+ fprintf(dst, "%s", line[i]->data);
+ }
+ return self;
+}