2012-08-13 60 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 2012-08-13 14:00:45

+0

我已經將NSRange添加到nsmutable數組中。 – Ketan 2012-08-13 14:02:36

+0

NSRange不是一個Objective-C對象...如果你試圖添加NSRange,則顯示錯誤「NSRange類型的集合元素(又名_NSRange)不是一個Objective-C對象 – 2012-08-13 14:02:54

回答

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"); 
    } ];