Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 按日期的2个属性对对象数组进行排序_Ios_Objective C_Sorting_Nsarray - Fatal编程技术网

Ios 按日期的2个属性对对象数组进行排序

Ios 按日期的2个属性对对象数组进行排序,ios,objective-c,sorting,nsarray,Ios,Objective C,Sorting,Nsarray,我有一个NSArray的CoreData对象,里面有两个属性:月和年。我想先按年然后按月对数组进行排序 DbDate 1: 月份:04 年份:2010年 DbDate 2: 月份:04 年份:2012年 dbdate3: 月份:06 年份:2011年 dbdate4: 月份:05 年份:2015年 我想将它们排序到数组中: DbDate 1 数据库日期3 数据库日期2 DbDate 4 我该怎么做?您可以先阅读NSArray的文档。什么,文档?它告诉你所有你可以用NSArray做的事情。我知道N

我有一个
NSArray
CoreData
对象,里面有两个属性:月和年。我想先按年然后按月对数组进行排序

DbDate 1:
月份:04
年份:2010年

DbDate 2:
月份:04
年份:2012年

dbdate3:
月份:06
年份:2011年

dbdate4:
月份:05
年份:2015年

我想将它们排序到数组中:

DbDate 1
数据库日期3
数据库日期2
DbDate 4


我该怎么做?

您可以先阅读NSArray的文档。什么,文档?它告诉你所有你可以用NSArray做的事情。我知道NSArray的分类方法,我的问题是我不知道怎么做,哪一个适合这个问题。我请求帮助是因为我不知道,不是因为我懒惰。如果你不想帮忙,那没关系。就你需要学习使用它的数量而言,最容易使用的方法可能是
sortedarrayingfunction
。(它也可能是最快的,或者至少是最快的。)您可以编写一个简单的C函数来定义您想要进行的比较。(尝试一些东西,并向我们展示您所尝试的。)
    // If you have an array:
NSArray *arrayOfObject;

NSSortDescriptor *sortByYear = [NSSortDescriptor sortDescriptorWithKey:@"year" ascending:YES];
NSSortDescriptor *sortByMonth = [NSSortDescriptor sortDescriptorWithKey:@"month" ascending:YES];
NSArray *sorts = @[sortByYear, sortByMonth];

NSArray *orderedArray = [arrayOfObject sortedArrayUsingDescriptors:sorts];

// If you want fetch the object ordered, from Core Data.
NSFetchRequest  *yourFetchReques = [[NSFetchRequest alloc] init];
// Set your entity...
[yourFetchReques setSortDescriptors:sorts];
// .... Your code to make the request...