2011-11-05 239 views
1

我在下面的格式,我在一個NSArray的解析和存儲接收JSON:如何對包含嵌套JSON字符串的NSSArray進行排序?

{"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"]} 

我如何得到它的「創造」進行排序? 「創造」具有似乎是時間的日期和用途。

任何指導將不勝感激。

+0

你是如何解析JSON?解析器返回什麼類型的對象? –

回答

2

使用sortedArrayUsingFunction:context:您將需要一個從您的JSON值返回「Sec」的方法。

寫一個比較函數是這樣的:

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; 
} 
0

使用sortedArrayUsing...方法之一。或者(恐怖恐怖)寫你自己的代碼進行排序。

相關問題