@@ -37,12 +37,22 @@ @interface OFMutableArray_placeholder: OFMutableArray @end static void -quicksort(OFMutableArray *array, size_t left, size_t right) +quicksort(OFMutableArray *array, size_t left, size_t right, int options) { + of_comparison_result_t ascending, descending; + + if (options & OF_SORT_OPTIONS_DESCENDING) { + ascending = OF_ORDERED_DESCENDING; + descending = OF_ORDERED_ASCENDING; + } else { + ascending = OF_ORDERED_ASCENDING; + descending = OF_ORDERED_DESCENDING; + } + while (left < right) { size_t i, j; id pivot; i = left; @@ -49,33 +59,32 @@ j = right - 1; pivot = [array objectAtIndex: right]; do { while ([[array objectAtIndex: i] compare: pivot] != - OF_ORDERED_DESCENDING && i < right) + descending && i < right) i++; while ([[array objectAtIndex: j] compare: pivot] != - OF_ORDERED_ASCENDING && j > left) + ascending && j > left) j--; if (i < j) [array exchangeObjectAtIndex: i withObjectAtIndex: j]; } while (i < j); - if ([[array objectAtIndex: i] compare: pivot] == - OF_ORDERED_DESCENDING) + if ([[array objectAtIndex: i] compare: pivot] == descending) [array exchangeObjectAtIndex: i withObjectAtIndex: right]; if (i > 0) { if (i > left && i - 1 - left > right - i - 1) { right = i - 1; - quicksort(array, i + 1, right); + quicksort(array, i + 1, right, options); } else { - quicksort(array, left, i - 1); + quicksort(array, left, i - 1, options); left = i + 1; } } else left = i + 1; } @@ -377,16 +386,21 @@ } } - (void)sort { + [self sortWithOptions: 0]; +} + +- (void)sortWithOptions: (int)options +{ size_t count = [self count]; if (count == 0 || count == 1) return; - quicksort(self, 0, count - 1); + quicksort(self, 0, count - 1, options); } - (void)reverse { size_t i, j, count = [self count];