分类和匹配
NSMutableArray *full_text_list = [[NSMutableArray alloc]init];
[full_text_list addObject:@"for"];
[full_text_list addObject:@"for your information"];
[full_text_list addObject:@"you"];
[full_text_list addObject:@"at"];
NSMutableArray *short_text_list = [[NSMutableArray alloc]init];
[short_text_list addObject:@"4"];
[short_text_list addObject:@"fyi"];
[short_text_list addObject:@"u"];
[short_text_list addObject:@"@"];
我不想排序第二个数组。我想获得基于索引的适当元素。 我想基于长度排序只有full_text_list阵列,所以我尝试了以下分类和匹配
NSSortDescriptor * descriptors = [[[NSSortDescriptor alloc] initWithKey:@"length"
ascending:NO] autorelease];
NSArray * sortedArray = [full_text_list sortedArrayUsingDescriptors:
[NSArray arrayWithObject:descriptors]];
和上面的代码工作fine.But我不知道如何short_text_list阵列新数组排序匹配
So when doing like [full_text_list objectatindex:0] and [short_text_list objectatindex:0] will not match
result would be "for your information" and "for" but the result should be "for your information" and "fyi"
请让我知道
做到这将是第二种方法:
// Create your two arrays and then combine them into one dictionary:
NSDictionary *textDict = [NSDictionary dictionaryWithObjects:short_text_list
forKeys:full_text_list];
// Create your sorted array like you did before:
NSSortDescriptor * descriptors = [[[NSSortDescriptor alloc] initWithKey:@"length" ascending:NO] autorelease];
NSArray * sortedArray = [full_text_list sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptors]];
// Then to access short_text, you would use:
NSString *object0ShortText = [textDict objectForKey:[sortedArray objectAtIndex:0]];
它应该如何匹配?你有两个数组,只是排序一个,并期望第二个自动获取排序呢?这是行不通的。你为什么不建立一个字典,把长信息作为关键字,将短信息作为价值或vs?
我不想要第二个数组进行排序。我想根据索引 – user198725878 2012-03-21 05:49:27
获得相应的元素。您认为fyi会如何进入第二个列表的顶部?它肯定会停留在索引1(假定addObject add在最后)。你怎么能期望第二个数组被任何对第一个数组完成的操作所触动? – Friedrich 2012-03-21 05:57:01
感谢您的回复。这两个数组都是动态的,并且来自db.when根据长度对full_text_list数组进行排序时,顺序可能会发生变化。这两个数组就像是一个密钥对值。如果full_text_list顺序发生变化,意味着我无法与short_text_list匹配。 – user198725878 2012-03-21 06:01:12
我会创建一个包含两个值的新类,并插入到阵列,而不是首先创建两个单独的阵列的组成:
@interface TextList : NSManagedObject
@property (nonatomic, retain) NSString *full_text;
@property (nonatomic, retain) NSString *short_text;
- (TextList *)initWithFullText:(NSString *)full_text shortText:(NSString *)short_text;
@end
创建.m文件,然后当你想使用它,使用这样的:
NSMutableArray *full_text_list = [[NSMutableArray alloc]init];
[full_text_list addObject:[TextList initWithFullText:@"for" shortText:@"4"]];
[full_text_list addObject:[TextList initWithFullText:@"for your information" shortText:@"fyi"]];
[full_text_list addObject:[TextList initWithFullText:@"you" shortText:@"u"]];
[full_text_list addObject:[TextList initWithFullText:@"at" shortText:@"@"]];
然后进行排序:
NSSortDescriptor * descriptors = [[[NSSortDescriptor alloc] initWithKey:@"full_text.length" ascending:NO] autorelease];
NSArray * sortedArray = [full_text_list sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptors]];
现在你可以做[[sortedArray objectAtIndex:0] full_text];
和[[sortedArray objectAtIndex:0] short_text];
或
TextList *txtList = [sortedArray objectAtIndex:0];
// txtList.full_text and txtList.short_text are both valid.
这里我们需要怎样排序? – user198725878 2012-03-21 06:35:06
使用以前使用的完全相同的代码。实际上,您并未在此处对字典进行排序,只是将原始的full_text_list和使用您创建的sortedArray进行排序。然后,查找字典中的full_text_list值(即sortedArray中的值)以查找与之匹配的short_text_list值。 – lnafziger 2012-03-21 06:38:19
我刚更新了这个例子。 – lnafziger 2012-03-21 06:40:22