排序字符串包含数字,NSMutableArray包含字典

时间:2014-03-05 17:35:24

标签: ios objective-c sorting nsmutablearray nsdictionary

字符串

"4 Miles 400 stones"
"2 Miles 10 stones"
"6 Miles 2 Stones" 

NsMutableArray中词典的一个关键值,我试图按照里程数对它们进行排序。

常规sortUsingDescriptor

[list sortUsingDescriptors:[NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:@"systems" ascending:YES], nil]];

NSNumericSearch

NSMutableArray *newList;
        NSArray *result = [list sortedArrayUsingFunction:&sort context:@"systems"];
        newList= [[NSMutableArray alloc] initWithArray:result];

NSInteger sort(id a, id b, void* p) {
    return [[a valueForKey:(__bridge NSString*)p]
            compare:[b valueForKey:(__bridge NSString*)p]
            options:NSNumericSearch];
}

无效。

我是否必须解析字符串获取数字然后对其进行排序?或者有一种更简单的方法来排序吗?

2 个答案:

答案 0 :(得分:1)

以下是如何使用最佳,面向对象的方式。

首先。创建一个类。我们称之为MyObject

@interface MyObject : NSObject
@property(nonatomic, assign) NSUInteger miles;
@property(nonatomic, assign) NSUInteger stones;

+ (MyObject *)objectWithString:(NSString *)string;

@end

正如您所看到的,它有一个objectWithString,我们将使用这些字符串中的信息创建对象,如:"4 Miles 400 stones"

@implementation MyObject

+ (MyObject *)objectWithString:(NSString *)string
{
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[0-9]+?(?= Miles | stones)" options:0 error:nil];
    NSArray *matches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];

    MyObject *myObject = [[MyObject alloc] init];

    myObject.miles = [[string substringWithRange:((NSTextCheckingResult *)matches[0]).range] integerValue];
    myObject.stones = [[string substringWithRange:((NSTextCheckingResult *)matches[1]).range] integerValue];

    return myObject;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"%d miles, %d stones", self.miles, self.stones];
}

@end

然后,我们将使用 NSSortDescriptor 对我们的数组进行排序:

MyObject *myObject1 = [MyObject objectWithString:@"4 Miles 400 stones"];
MyObject *myObject2 = [MyObject objectWithString:@"2 Miles 10 stones"];
MyObject *myObject3 = [MyObject objectWithString:@"6 Miles 2 stones"];

NSArray *array = @[myObject1, myObject2, myObject3];

NSSortDescriptor *miles = [[NSSortDescriptor alloc] initWithKey:@"miles" ascending:YES];
NSSortDescriptor *stones = [[NSSortDescriptor alloc] initWithKey:@"stones" ascending:YES];
NSArray *sortDescriptors = @[miles, stones];

NSArray *sortedArray = [array sortedArrayUsingDescriptors:sortDescriptors];

NSLog(@"Sorted: %@", sortedArray);

输出:

2014-03-05 19:51:54.233 demo[12267:70b] Sorted: (
    "2 miles, 10 stones",
    "4 miles, 400 stones",
    "6 miles, 2 stones" )

它像我朋友的魅力一样!

答案 1 :(得分:0)

你的方法是正确的,

我已经尝试过你的方式了

NSInteger sort(id a, id b, void* p) {
    return [[a valueForKey:(__bridge NSString*)p]
            compare:[b valueForKey:(__bridge NSString*)p]
            options:NSNumericSearch];
}

正确排序确保你调用正确的字典值或正确的方法,放一些断点,你可能会发送空值或其他东西。

如果您想要反向搜索,请使用

NSInteger reverseSort(id a, id b, void* p) {
    return - [[a valueForKey:(__bridge NSString*)p]
            compare:[b valueForKey:(__bridge NSString*)p]
            options:NSNumericSearch];
}