1

我在排序数组时遇到问题。我有两个问题。1.使用以下代码对rightArray进行排序不起作用。2.我还有一个leftArray,当对右数组进行排序时,它的索引应该与右数组相比发生变化。这可能吗?

NSArray *rightArray = [[NSArray alloc] initWithObjects:[mutArray objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 11)]], nil];
NSArray *sortedArray = [rightArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
NSLog(@"%@",sortedArray);

输出:在我使用上面的代码排序之后

2012-10-25 19:11:44.571 Converter[3511:207] (
    (
    USD,
    EUR,
    GBP,
    JPY,
    CAD,
    AUD,
    INR,
    CHF,
    CNY,
    KWD,
    SGD
)
)
4

2 回答 2

3
NSArray *rightArray = [[NSArray alloc] initWithObjects:[mutArray objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 11)]], nil];
NSArray *sortedArray = [[rightArray lastObject] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
NSLog(@"%@",sortedArray);

Your strings are in an array in your array. Get the only object in rightArray and sort it.

edit

For the second question you can use an intermediate data structure

NSMutableDictionary *temp = [[NSMutableDictionary alloc] init];
NSArray *keys = [rightArray lastObject]; 
NSArray *objs = [leftArray lastObject]; // presuming they're also an array in an array

for (int i = 0; i < [keys count]; i++) {
    // we'll use the dictionary to set an one-to-one relationship
    [temp setObject:[objs objectAtIndex:i] forKey:[keys objectAtIndex:i]];
}

NSArray *sortedKeys = [keys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
NSArray *sortedObjs = [temp objectsForKeys:sortedKeys notFoundMarker:[NSNull null]];
[temp release]; // if not using ARC
于 2012-10-25T14:09:26.160 回答
0
NSArray *array =  @[@"USD",@"EUR",@"GBP",@"JPY",@"CAD",@"AUD",@"INR",@"CHF",@"CNY",@"KWD",@"SGD"];
NSArray *sortedArray = [array sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
NSLog(@"SortedArray:%@",sortedArray);

它将输出:

SortedArray:(
    AUD,
    CAD,
    CHF,
    CNY,
    EUR,
    GBP,
    INR,
    JPY,
    KWD,
    SGD,
    USD
)

正如亚历山大回答的那样,您没有传递字符串数组。

于 2012-10-25T14:17:55.270 回答