Index: src/OFArray.h ================================================================== --- src/OFArray.h +++ src/OFArray.h @@ -28,10 +28,14 @@ #import "OFEnumerator.h" #import "OFSerialization.h" #import "OFJSONRepresentation.h" @class OFString; + +enum { + OF_SORT_OPTIONS_DESCENDING = 1 +}; #ifdef OF_HAVE_BLOCKS typedef void (^of_array_enumeration_block_t)(id object, size_t index, BOOL *stop); typedef BOOL (^of_array_filter_block_t)(id odject, size_t index); @@ -263,10 +267,22 @@ * * @return A sorted copy of the array */ - (OFArray*)sortedArray; +/*! + * @brief Returns a sorted copy of the array. + * + * @param options The options to use when sorting the array.@n + * Possible values are: + * Value | Description + * ---------------------------|------------------------- + * OF_SORT_OPTIONS_DESCENDING | Sort in descending order + * @return A sorted copy of the array + */ +- (OFArray*)sortedArrayWithOptions: (int)options; + /*! * @brief Returns a copy of the array with the order reversed. * * @return A copy of the array with the order reversed */ Index: src/OFArray.m ================================================================== --- src/OFArray.m +++ src/OFArray.m @@ -580,10 +580,21 @@ { OFMutableArray *new = [[self mutableCopy] autorelease]; [new sort]; + [new makeImmutable]; + + return new; +} + +- (OFArray*)sortedArrayWithOptions: (int)options +{ + OFMutableArray *new = [[self mutableCopy] autorelease]; + + [new sortWithOptions: options]; + [new makeImmutable]; return new; } Index: src/OFMutableArray.h ================================================================== --- src/OFMutableArray.h +++ src/OFMutableArray.h @@ -148,10 +148,21 @@ /*! * @brief Sorts the array. */ - (void)sort; +/*! + * @brief Sorts the array. + * + * @param options The options to use when sorting the array.@n + * Possible values are: + * Value | Description + * ---------------------------|------------------------- + * OF_SORT_OPTIONS_DESCENDING | Sort in descending order + */ +- (void)sortWithOptions: (int)options; + /*! * @brief Reverts the order of the objects in the array. */ - (void)reverse; Index: src/OFMutableArray.m ================================================================== --- src/OFMutableArray.m +++ src/OFMutableArray.m @@ -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]; Index: tests/OFArrayTests.m ================================================================== --- tests/OFArrayTests.m +++ tests/OFArrayTests.m @@ -151,11 +151,14 @@ m[1] = [[a[0] mutableCopy] autorelease]; [m[1] addObject: @"0"]; [m[1] addObject: @"z"]; TEST(@"-[sortedArray]", [[m[1] sortedArray] isEqual: ([OFArray arrayWithObjects: - @"0", @"Bar", @"Baz", @"Foo", @"z", nil])]) + @"0", @"Bar", @"Baz", @"Foo", @"z", nil])] && + [[m[1] sortedArrayWithOptions: OF_SORT_OPTIONS_DESCENDING] + isEqual: ([OFArray arrayWithObjects: + @"z", @"Foo", @"Baz", @"Bar", @"0", nil])]) EXPECT_EXCEPTION(@"Detect out of range in -[objectAtIndex:]", OFOutOfRangeException, [a[0] objectAtIndex: [a[0] count]]) EXPECT_EXCEPTION(@"Detect out of range in -[removeObjectsInRange:]",