I am getting date/time as JSON that I am storing as a series of NSArray objects.
The JSON format is as follows:
"created_at" = "2012-09-21T12:41:26-0500"
How do I sort my array so that the latest items are at the very top.
Thank You
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The first thing you need to do is convert the date string into an
NSDateobject for every one of the items inside your original array, this is key, as the system knows how to compare dates, but not ISO8601 strings.There are several ways to do the conversion, in the example bellow I am using a custom category on NSDate, you can use the answer to this question for the conversion: Is there a simple way of converting an ISO8601 timestamp to a formatted NSDate?,
Once you have
NSDates, sorting is extremely easy using anNSSortDescriptoras shown bellow:The variable
sortedArraycontains a sorted version of your original array, and as you can see we pass an array of sort descriptors, this allows you to pass multiple sort descriptors, in case you want to sort basen on the date and another property (e.g. title, name) as a secodnary descriptor.