Sort NSArray by date and time

空扰寡人 提交于 2019-12-10 12:04:50

问题


This may be a duplicate question, but i have a strange issue, So that i have pasted it on stack.

I have a issue while sorting NSArray by date and time.

For example, I have below fields in Database file:-

id        Name       Date
1         ABC        2015-01-29 16:36:31
2         AAA        2015-01-29 16:36:31
3         BBB        2015-01-29 16:36:31
4         CCC        2015-01-29 16:36:31
5         DDD        2015-01-29 16:36:31

Note :- Datatype of Date filed is Timestamp in sqlite Database and i am storing it as NSDate while fetching data.

Now, I am sorting this data as below through NSArray

NSSortDescriptor *sortDesc = [[NSSortDescriptor alloc]initWithKey:kContentEditedDate ascending:NO];
        NSArray *sortDescAry = [NSArray arrayWithObject:sortDesc];
        contentData = [contentData sortedArrayUsingDescriptors:sortDescAry];

Above code just display descending order even each row has same date.

After that i also tried [NSArray sortedArrayUsingComparator:] to sorting it also display same result as first option.

I tried to edit date of last row in database and run query as below:

SELECT RelImgName,EditedDate FROM "tableName"  ORDER BY "EditedDate" DESC;

it give me proper output. As Below:

id        Name       Date
 5         DDD        2015-02-02 11:05:32
 1         ABC        2015-01-29 16:36:31
 2         AAA        2015-01-29 16:36:31
 3         BBB        2015-01-29 16:36:31
 4         CCC        2015-01-29 16:36:31

But while i am doing this with NSSortDescriptor it display only descending order.

I want output as above with NSSortDescriptor.

Now i hope you all can understand my question.

I search a lot but didn't get any proper solution for it.

来源:https://stackoverflow.com/questions/28271322/sort-nsarray-by-date-and-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!