Tablica sortowania (NSArray) w porządku malejącym
Mam tablicę obiektów NSString, które muszę sortować malejąco.
Ponieważ nie znalazłem żadnego interfejsu API do sortowania tablicy w porządku malejącym, zbliżyłem się w następujący sposób.
Napisałem kategorię dla NSString, jak podano poniżej.
- (NSComparisonResult)CompareDescending:(NSString *)aString
{
NSComparisonResult returnResult = NSOrderedSame;
returnResult = [self compare:aString];
if(NSOrderedAscending == returnResult)
returnResult = NSOrderedDescending;
else if(NSOrderedDescending == returnResult)
returnResult = NSOrderedAscending;
return returnResult;
}
Następnie posortowałem tablicę za pomocą instrukcji
NSArray *sortedArray = [inFileTypes sortedArrayUsingSelector:@selector(CompareDescending:)];
Czy to właściwe rozwiązanie? czy jest lepsze rozwiązanie?