iphone - 带数字的字符串排序 last objective c

标签 iphone objective-c string sorting compare

我对 SO 进行了很好的搜索,但找不到与此问题相关的任何内容。我有一个自定义对象类,它实现了一种用于在数组中排序的比较方法。此比较使用使用 NSCaseInsensitiveSearch 的类的“名称”属性。代码如下所示:

- (NSComparisonResult)compare:(Forum *)otherObject {
return [self.name compare:otherObject.name options:(NSCaseInsensitiveSearch)];
}

然而,使用 NSCaseInsensitiveSearch 会将带有数字的字符串放在带有字母的字符串之前。有没有办法做相反的事情,让数字出现在字母之后?

最佳答案

不要只是

return [self.name compare:otherObject.name options:(NSCaseInsensitiveSearch)];

但是做一些额外的检查,例如如果 self.name 以非数字开头,而 otherObject.name 以数字开头,则返回 NSDescendingOrder,反之亦然。 IOW,做一个数字>非数字。如果两者都是非数字或都是数字,请返回您已有的内容。

这个小控制台程序解释了我的意思:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSMutableArray *array = [NSMutableArray arrayWithObjects: 
                             @"Adam", @"001", @"Bertha", @"3SeriesBMW", @"Colin",
                             @"Zelda", @"1And1", @"Xaver", @"Kraftwerk", @"TangerineDream",
                             @"5SeriesBMW", @"0ableTypes", nil];

    NSString *sortOrder = @"AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz_0123456789";

    [array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) 
    {
        char char1 = [(NSString *)obj1 characterAtIndex: 0];
        char char2 = [(NSString *)obj2 characterAtIndex: 0];

        int index1;
        for (index1 = 0; index1 < sortOrder.length; index1++)
            if ([sortOrder characterAtIndex: index1] == char1)
                break;

        int index2;
        for (index2 = 0; index2 < sortOrder.length; index2++) 
            if ([sortOrder characterAtIndex: index2] == char2)
                break;

        if (index1 < index2)
            return NSOrderedAscending;
        else if (index1 > index2)
            return NSOrderedDescending;
        else
            return [(NSString *)obj1 compare: obj2 options: NSCaseInsensitiveSearch];
    }];

    for (NSString *s in array)
        NSLog(@"%@", s);

    [pool drain];
    return 0;
}

输出是:

2011-08-25 14:19:15.538 NumeralSort[5802:707] Adam
2011-08-25 14:19:15.540 NumeralSort[5802:707] Bertha
2011-08-25 14:19:15.540 NumeralSort[5802:707] Colin
2011-08-25 14:19:15.540 NumeralSort[5802:707] Kraftwerk
2011-08-25 14:19:15.541 NumeralSort[5802:707] TangerineDream
2011-08-25 14:19:15.541 NumeralSort[5802:707] Xaver
2011-08-25 14:19:15.541 NumeralSort[5802:707] Zelda
2011-08-25 14:19:15.542 NumeralSort[5802:707] 001
2011-08-25 14:19:15.542 NumeralSort[5802:707] 0ableTypes
2011-08-25 14:19:15.542 NumeralSort[5802:707] 1And1
2011-08-25 14:19:15.543 NumeralSort[5802:707] 3SeriesBMW
2011-08-25 14:19:15.543 NumeralSort[5802:707] 5SeriesBMW

这不完全是解决方案,但应该可以帮助您。

关于iphone - 带数字的字符串排序 last objective c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7189090/

相关文章:

iphone - 滑动后 UITableViewCell 上的自定义按钮

objective-c - (Cocoa Mac) NSWindowController showWindow 分配/初始化一个新的 NSWindowController?

asp.net-mvc - Angular http get 在额外的引号中返回字符串

javascript 字符串解释为对象

iphone - 影响应用程序性能的 AVAudioPlayer 声音

ios - App 和 InApp Purchase 第二次被拒绝

iphone - 如何在 FMDatabase 中打开外键?

iphone - 使用委托(delegate)模式时避免 EXC_BAD_ACCESS

ios - 使用其他选项卡时 tabbarController 中的 UiTableView 向下移动

linux - 解析命令输出