所以我有一个数组,我从一个Web服务检索没有特定的顺序
So i have an array that i retrieve from a web service in no particular order
例如:
0 => x large, 1 => large, 2 => XX large, 3 => small, 4 => medium, 5 => x small我需要对它们进行排序:首先根据特定的 - 可以颠倒字母: p>
I need to sort them: firstly based on specific - which could be reverse alphabetic:
small medium large其次,我需要根据他们的x计数器部分对它们进行排序:
Secondly i need to sort them based on their 'x' counter parts:
x small small medium large x large xx large$ b b
我知道我可以用暴力字符串匹配,但我真的想建议如何做这个整洁,也许是一个正则表达式或更优雅的东西?
I know i can do this with brute force string matching but i would really like a suggestion on how to do this tidily, perhaps a regex or something more elegant?
推荐答案使用 NSComparator 块语法。像
NSArray * sizes = [NSArray arrayWithObjects: @"x small",@"small",@"medium",@"large",@"x large", nil]; NSArray *sortedBySizes =[array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { if ([sizes indexOfObject:[obj1 size]]> [sizes indexOfObject:[obj2 size]]) return (NSComparisonResult)NSOrderedAscending; if ([sizes indexOfObject:[obj1 size]]< [sizes indexOfObject:[obj2 size]]) return (NSComparisonResult)NSOrderedDescending; return (NSComparisonResult)NSOrderedSame; }];
在第二种方法中,由web服务器发送的数字和x尺寸。现在 [obj size]; 是假设返回一个NSNumber对象。
In the second approach I added a mapping between the numbers send by the web server and the x-sizes. Now [obj size]; is suppose to return a NSNumber object.
NSArray * sizesStrings = [NSArray arrayWithObjects: @"x small",@"small", @"medium",@"large", @"x large",@"xx large", nil]; NSArray * sizesNumbers = [NSArray arrayWithObjects:[NSNumber numberWithInt:5], [NSNumber numberWithInt:3], [NSNumber numberWithInt:4], [NSNumber numberWithInt:1], [NSNumber numberWithInt:0], [NSNumber numberWithInt:2], nil]; NSDictionary *sizes = [NSDictionary dictionaryWithObjects:sizesStrings forKeys:sizesNumbers]; NSArray *sortedBySizes = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { NSString *sizeObj1String = [sizes objectForKey:[obj1 size]]; NSString *sizeObj2String = [sizes objectForKey:[obj1 size]]; int i1 = [sizesStrings indexOfObject:sizeObj1String]; int i2 = [sizesStrings indexOfObject:sizeObj2String]; if (i1 > i2) return (NSComparisonResult)NSOrderedAscending; if (i2 > i1) return (NSComparisonResult)NSOrderedDescending; return (NSComparisonResult)NSOrderedSame; }];
问题的第二个任务 - 小,中,大 - 可以这样做:
The second task of the question — the grouping into small, medium, large — could be done like this:
NSDictionary *groups = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSMutableArray array],[NSMutableArray array],[NSMutableArray array], nil] forKeys:[NSArray arrayWithObjects:@"small",@"medium",@"large",nil] ]; [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { int i = [[obj size] intValue]; if (i == 5 || i == 3) [[groups objectForKey:@"small"] addObject:obj]; else if (i == 2 || i == 0 || i == 1) [[groups objectForKey:@"large"] addObject:obj]; else [[groups objectForKey:@"medium"] addObject:obj]; }];注意:直接键入的未经测试的代码。 sup>
Note: Codes untested, as directly typed.
更多推荐
排序nsarray的字符串,而不是基于字符串
发布评论