2015-04-02 103 views
0

我有一个NSMutableArray的对象。我需要根据createdDate和name对它们进行排序。 以下是我正在面对的场景: 文件的名称类似于: Image.pdf,Image 1.pdf,Image 2.pdf,Image 3.pdf,Image 4.pdf,Image 5.pdf ...使用NSSortDescriptor对NSMutableArray iOS中的对象进行排序?

现在这些文件可以有相同的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; 

回答

0

你有我猜想错误的逻辑,首先你必须检查所有文件是否有相同的日期,如果他们有相同的日期,然后只按名称排序,否则按日期排序。

+0

您可以添加更多的洞察力,如何才能实现。 – 2015-04-02 07:09:11

+0

首先,你需要改变你的问题,并使其更具可读性并使其理解,以便我可以提出一些建议。 – Retro 2015-04-02 07:11:26

+0

让我说我得到3个文件的日期2015年4月2日与名称:Image.pdf,图片1.pdf,图片3.pdf排序顺序:图片3.pdf,图片1.pdf,Image.pdf现在我得到4日期03-ape-2015的更多文件名称:Image 7.pdf,Image 8.pdf,Image 9.pdf,Image 2.pdf排序顺序:Image 9.pdf,Image 8.pdf,Image 7.pdf,Image2 .pdf,Image 3.pdf,Image 1.pdf,Image.pdf – 2015-04-02 15:55:38

0

整数的字符串表示给出未定义的行为,所以当您比较@"Image 1.pdf" < @"Image 2.pdf"时,将会有未定义的行为。

你可以做的是尝试先获取附加到“图像”的数字,然后尝试对其进行排序。

相关问题