首先我想对我糟糕的英语表示抱歉。希望您能理解。
我搜索了三天多的时间来寻找我的问题,但没有找到解决方案:( 我有一个包含 NSMutablearrays 的 NSMutablearray。
我从网络上获取数据。网络输出如下所示:
( ("Restaurant2" , "10" , "Hotel" , "Solitudestr" , "Germany" , "48.81155" , "9.10903"),("Club 2" , "14" , "Club" , "Weilimdorfer213" , "Germany" , "48.814" , "9.1311666666667"),("Thai Meat" , "22" , "Gastro" , "Weilimdorfer193" , " 70469 Stuttgart" , "48.813833333333" , "9.1328333333333") )
所以我在一个 NSMutablearray 里面有 3 个 NSMutablearray。我正在使用自定义单元格 View 在我的 Tableview 中显示数组。
我的问题是如何使用第一个值对三个内部数组进行排序。 (第一个值的值:Restaurant2、Club 2、Thai Meat)
所以第一个数组应该是:
("Club 2" , "14" , "Club" , "Weilimdorfer213" , "Germany" , "48.814" , "9.1311666666667")
第二个:
("Restaurant2" , "10" , "Hotel" , "Solitudestr" , "Germany" , "48.81155" , "9.10903")
第三:
("Thai Meat" , "22" , "Gastro" , "Weilimdorfer193" , " 70469 Stuttgart" , "48.813833333333" , "9.1328333333333")
我应该使用哪种排序函数以及如何使用?
在第二步中,我想获取给定坐标的距离并对到当前位置的距离进行排序。
我已经知道距离了。但如何排序呢?
非常感谢!
最佳答案
如果您要创建一个自定义对象来存储信息,而不是使用数组,这会容易得多。
看看 Dave DeLong 编写的这个示例在 this发布。
//ScoreRecord.h
@interface ScoreRecord : NSObject {
NSString * label;
NSUInteger score;
}
@property (nonatomic, retain) NSString * label;
@property (nonatomic) NSUInteger score;
@end
//ScoreRecord.m
#import "ScoreRecord.h"
@implementation ScoreRecord
@synthesize label, score;
- (void) dealloc {
[score release];
[super dealloc];
}
@end
//elsewhere:
NSMutableArray * scores = [[NSMutableArray alloc] init];
ScoreRecord * first = [[ScoreRecord alloc] init];
[first setLabel:@"Label 1"];
[first setScore:1];
[scores addObject:first];
[first release];
//...etc for the rest of your scores
填充 scores
数组后,您现在可以执行以下操作:
//the "key" is the *name* of the @property as a string. So you can also sort by @"label" if you'd like
NSSortDescriptor * sortByScore = [NSSortDescriptor sortDescriptorWithKey:@"score" ascending:YES];
[scores sortUsingDescriptors:[NSArray arrayWithObject:sortByScore]];
此后,您的 scores
数组将按分数升序排序。
关于xcode - 如何对二维多维 NSMutablearray 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9163095/