排序nsarray的字符串,而不是基于字符串

编程入门 行业动态 更新时间:2024-10-26 10:38:38
本文介绍了排序nsarray的字符串,而不是基于字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以我有一个数组,我从一个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的字符串,而不是基于字符串

本文发布于:2023-06-02 12:41:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/447356.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   而不   是基于   nsarray

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!