SQLite數據庫中有下面的數組:
(
{
id=1;
duedate = "01-Apr-2013";
},
{
id=2;
duedate = "01-May-2013";
},
{
id=3;
duedate = "01-Jun-2013";
},
{
id=4;
duedate = "11-Jul-2013";
}
)
排序的代碼:
NSSortDescriptor *sortDesc = [[NSSortDescriptor alloc] initWithKey:@"duedate" ascending:YES];
[assessment_list sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];
但是最後出現的排序不對:
(
{
id=1;
duedate = "01-Apr-2013";
},
{
id=3;
duedate = "01-Jun-2013";
},
{
id=2;
duedate = "01-May-2013";
},
{
id=4;
duedate = "11-Jul-2013";
}
)
不知道為什麼?
因為它是按照字母順序排列的
想根據日期排序,用下面的方法:
NSInteger dateSort(id dict1, id dict2, void* context){
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"mm/dd/yyyy"];
NSDate *date1 = [formatter dateFromString:[dict1 objectForKey:@"duedate"]];
NSDate *date2 = [formatter dateFromString:[dict2 objectForKey:@"duedate"]];
return [[date1 compare:date2];
}
調用函數:
[dateArray sortUsingFunction:dateSort context:NULL];