Objective c 在Objective C中对2D数组进行排序
我一直在用objective c对2D数组进行排序。我有一个2D数组,由两个字符串数组组成 NSArray*totalRatings=[NSArray arrayWithObjects:namesArray,ratingsArray,nil] 我使用以下方法提取了这些值:Objective c 在Objective C中对2D数组进行排序,objective-c,sorting,multidimensional-array,2d,Objective C,Sorting,Multidimensional Array,2d,我一直在用objective c对2D数组进行排序。我有一个2D数组,由两个字符串数组组成 NSArray*totalRatings=[NSArray arrayWithObjects:namesArray,ratingsArray,nil] 我使用以下方法提取了这些值: for (int i=0; i<2; i++) { for (int j=0; j<5; j++) { NSString *strings = [[totalRatings objectAtIndex
for (int i=0; i<2; i++) {
for (int j=0; j<5; j++) {
NSString *strings = [[totalRatings objectAtIndex:i] objectAtIndex:j];
NSLog(@"i=%i, j=%i, = %@ \n",i,j,strings);
}
}
既然这些都是字符串,如何对它们进行排序?我正在寻找:
Person 1, 12
Person 4, 10
Person 3, 9
Person 2, 5
Person 7, 2
如果您能提供任何帮助,我将不胜感激。您可能希望使用字典来处理这些数据,而不是使用两个不同的数组 你为什么不把每个人的记录放在字典上,而不是现在的记录呢
NSArray *totalRatings = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:@"Person 1", @"name", [NSNumber numberWithInt:12], @"rating", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Person 2", @"name", [NSNumber numberWithInt:5], @"rating", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Person 3", @"name", [NSNumber numberWithInt:9], @"rating", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Person 4", @"name", [NSNumber numberWithInt:10], @"rating", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Person 7", @"name", [NSNumber numberWithInt:2], @"rating", nil],
nil];
NSArray *sortedArray = [totalRatings sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"rating" ascending:NO]]];
NSArray *totalRatings = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:@"Person 1", @"name", [NSNumber numberWithInt:12], @"rating", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Person 2", @"name", [NSNumber numberWithInt:5], @"rating", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Person 3", @"name", [NSNumber numberWithInt:9], @"rating", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Person 4", @"name", [NSNumber numberWithInt:10], @"rating", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Person 7", @"name", [NSNumber numberWithInt:2], @"rating", nil],
nil];
NSArray *sortedArray = [totalRatings sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"rating" ascending:NO]]];