2012-08-13 2 views
0

NSMutable 배열 즉, [{5, 12} {0, 4} {18, 10}]에 범위 객체를 추가했습니다. 이제 NSMutable 배열을 오름차순으로 정렬하려고합니다. NSMutable 배열 객체를 순서대로 가져 오려면 다음 코드를 사용하고 있습니다.범위 객체가있는 NSMutablearray 정렬

 NSArray *stringIndexes = [StringIndexes copy]; 
     stringIndexes = [stringIndexes sortedArrayUsingSelector:@selector(compare:)]; 
     NSLog(@"stringIndexes..:%@",stringIndexes); 

그러나 필요한 출력을 제공하지 않습니다. 누구든지 범위 개체를 갖는 NSMutable 배열을 정렬하는 방법을 알고 있다면. 제발 도와주세요.

감사합니다.

+1

이 어떻게 변경 가능한 배열에'NSRange'을 추가 한을? 'NSRange'는 객체가 아닙니다. 구조체입니다. – deanWombourne

+0

NSRange를 nsmutable 배열에 추가했습니다. – Ketan

+0

NSRange가 Objective-C 객체가 아닙니다 ... NSRange를 추가하려고하면 "NSRange"(일명 _NSRange) 유형의 Collection 요소가 Objective-C 객체가 아닙니다. " –

답변

5

당신이 배열에 범위를 추가 할 경우 배열 정렬 할 때,

NSValue *value = [NSValue valueWithRange:(NSRange)range]; 

를 사용

[myArray sortUsingComparator:^NSComparisonResult(NSValue *val1, NSValue *val2, BOOL *stop) { 
    NSRange range1 = [val1 rangeValue]; 
    NSRange range2 = [val2 rangeValue]; 
    // you need to fine tune the test below - not sure what you want 
    if(range1.location == range2.location) return NSOrderedSame; 
    if(range1.location < range2.location) return NSOrderedAscending; 
    if(range1.location > range2.location) return NSOrderedDescending; 
    assert(!"Impossible"); 
    } ]; 
관련 문제