3

我有一个字母数字名称列表,当我运行下面的代码时,数字在A-Z (0-9 A-Z).

我怎样才能NSSortDescriptor把这个数字放在数组的末尾(A-Z 0-9)

NSSortDescriptor *sortNameSortDescriptor = 
[NSSortDescriptor sortDescriptorWithKey:@"sortName"
ascending:YES
selector:@selector(localizedStandardCompare:)];

NSArray *sortedArray = [sortSectionsArray sortedArrayUsingDescriptors:@[ sortNameSortDescriptor ]];
4

1 回答 1

2

也许您可以使用基于 NSComparator 的排序描述符?

粗略的轮廓

NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"sortName" ascending:YES
     comparator:^NSComparisonResult(NSString* obj1, NSString* obj2) 
     {
            //Return negative number, zero or positive based on 
            //first letter being number or alpha. 
            //If obj1 starts with a letter and obj2 starts with a number then
            //return NSOrderedAscending. . . otherwise just return [obj1 comppare:obj2];

            //To test if first chararacter is a letter:

            NSRange first = [obj1 rangeOfComposedCharacterSequenceAtIndex:0];
            NSRange match = [obj1 rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet] options:0 range:first];
            if (match.location != NSNotFound) {//Implement above comment}
     }];
于 2013-10-21T07:35:40.373 回答