从CSV文件中排序数组

时间:2014-06-11 09:02:12

标签: ios objective-c arrays sorting csv

我有一个CSV文件,我已经解析为我的xcode项目。

CSV文件是按随机排序的顺序,我希望最小的数字是第一个。

我不断收到一条警告,指出"不兼容的指针类型分配给' NSMutableArray *'来自' NSArray *'?我认为这与我试图对常规数组和可变数组进行排序有关吗? 这是我解析CSV文件的代码:

NSString *hejhej = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"csv"];
NSString *nyt = [NSString stringWithContentsOfFile:hejhej encoding:NSUTF8StringEncoding error:nil];
NSString *nytnyt = [nyt stringByReplacingOccurrencesOfString:@"\"" withString:@""];


NSMutableArray *row= [[nytnyt componentsSeparatedByString:@"\n,"]mutableCopy];
NSMutableArray *data = [NSMutableArray array];
for(NSString *line in row){
    NSMutableArray *points = [[line componentsSeparatedByString:@","] mutableCopy];

当我将其拖放到我的项目中时,我的文件看起来像这样(这只是测试数据):

"""00.52.36"",""5"""
,"""01.22.55"",""54"""
,"""01.14.54"",""8"""
,"""02.43.54"",""32"""
,"""03.43.02"",""11"""
,"""03.14.11"",""1"""
,"""06.23.32"",""15"""
,"""06.54.43"",""7"""
,"""07.32.12"",""2"""
,"""07.45.45"",""22"""
,"""08.11.22"",""43"""
,"""08.45.43"",""9"""
,"""09.00.11"",""222"""
,"""09.44.32"",""43"""
,"""10.21.34"",""321"""
,"""10.44.22"",""55"""
,"""11.01.02"",""21"""

我想先按最低值排序。所以它应该切换第2行和第3行,以及第5行和第6行。

我使用此代码对其进行排序,但它不起作用?

NSSortDescriptor *sd = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES];
points = [points sortedArrayUsingDescriptors: @[sd]];

1 个答案:

答案 0 :(得分:0)