1

I am receiving JSON in the following format that I parse and store in a NSArray:

{"4eb57e72c7e24c014f000000":{"_id":{"$id":"4eb57e72c7e24c014f000000"},"author":"tim","comments":[],"created":{"sec":1320517234,"usec":856000},"picture":"http://someurl.com","text":"this is a test","title":"test","type":["test"]}

How do I get it to sort by "created"? "created" has the sec which appears to be the date and usec that appears to be the time.

Any guidance will be appreciated.

1
  • How are you parsing the JSON? What kind of objects does the parser return? Commented Nov 5, 2011 at 23:31

2 Answers 2

2

Use sortedArrayUsingFunction:context: You'll need a method that returns "Sec" from your JSON value.

Write A comparison function something like:

NSInteger intSort(id num1, id num2, void *context)
{
    int v1 = [num1 getSecFromJSONValue];
    int v2 = [num2 getSecFromJSONValue];
    if (v1 < v2)
        return NSOrderedAscending;
    else if (v1 > v2)
        return NSOrderedDescending;
    else
        return NSOrderedSame;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Use one of the sortedArrayUsing... methods. Or (horror of horrors) write your own code to sort.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.