我有一个对象的NSMutableArray。我需要根据createdDate和name对它们进行排序。下面是我面对的场景:文件名是这样的: Image.pdf,图片1.pdf,图片2.pdf,图片3.pdf,图片4.pdf,图片5.pdf...
现在,这些文件可以具有相同的createdDate,因此当createdDate相同时,应该按名称降序排序。所以如果上面的文件有相同的创建日期,那么排序后它应该显示为:图像5.pdf,图像4.pdf,图像3.pdf,图像2.pdf,图像1.pdf,Image.pdf,但当我使用下面的代码时,Image.pdf是混乱的或显示如下: Image.pdf,图像5.pdf,图像4.pdf,图像3.pdf,图像3.pdf,图像1.pdf
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"createdDate" ascending:NO selector:@selector(compare:)];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO selector:@selector(localizedStandardCompare:)];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor1, sortDescriptor2, nil];
return sortDescriptors;发布于 2015-04-02 13:30:20
我猜你有错误的逻辑,首先你必须检查所有文件是否具有相同的日期,如果它们具有相同的日期,则仅按名称排序,否则按日期排序。
发布于 2015-04-02 13:30:59
整数的字符串表示提供了未定义的行为,因此当您比较@"Image 1.pdf" < @"Image 2.pdf"时,将具有未定义的行为。
你能做的就是首先尝试获取附加到"Image“后面的数字,然后尝试对其进行排序。
https://stackoverflow.com/questions/29405539
复制相似问题