2012-03-21 55 views
-1
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" 

請讓我知道

回答

0

做到這將是第二種方法:

// 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]]; 
+0

這裏我們需要怎樣排序? – user198725878 2012-03-21 06:35:06

+0

使用以前使用的完全相同的代碼。實際上,您並未在此處對字典進行排序,只是將原始的full_text_list和使用您創建的sortedArray進行排序。然後,查找字典中的full_text_list值(即sortedArray中的值)以查找與之匹配的short_text_list值。 – lnafziger 2012-03-21 06:38:19

+0

我剛更新了這個例子。 – lnafziger 2012-03-21 06:40:22

1

它應該如何匹配?你有兩個數組,只是排序一個,並期望第二個自動獲取排序呢?這是行不通的。你爲什麼不建立一個字典,把長信息作爲關鍵字,將短信息作爲價值或vs?

+0

我不想要第二個數組進行排序。我想根據索引 – user198725878 2012-03-21 05:49:27

+0

獲得相應的元素。您認爲fyi會如何進入第二個列表的頂部?它肯定會停留在索引1(假定addObject add在最後)。你怎麼能期望第二個數組被任何對第一個數組完成的操作所觸動? – Friedrich 2012-03-21 05:57:01

+0

感謝您的回覆。這兩個數組都是動態的,並且來自db.when根據長度對full_text_list數組進行排序時,順序可能會發生變化。這兩個數組就像是一個密鑰對值。如果full_text_list順序發生變化,意味着我無法與short_text_list匹配。 – user198725878 2012-03-21 06:01:12

0

我會創建一個包含兩個值的新類,並插入到陣列,而不是首先創建兩個單獨的陣列的組成:

@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.