2017-10-17 4 views
0

私はUITableViewUICollectionViewを内部に持っています。私はこれをDescend(Latest Post First)と並べ替えたいと思っていました。どのようにしたいのですか?私はいくつかの方法を試しましたが、すべてが機能していません。続きIDまたは月ごとに配列をソートする方法:降順(最新のポストファースト)?

は私のコードと応答

[Utilities serverRequest:@{@"getAnnouncement":@"a6dba37437ced2c3b07469fd6c0661f3"} completionBlock:^(id response) { 

    collectionViewDic[@"announcement"] = response[@"response"]; 
    _annArray = response[@"response"]; 
    NSLog(@"AnnouncementResponse%@",response); 

    // sorting to newest 
    /* 
    NSSortDescriptor *sortDescriptor; 
    sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"anc_id" ascending:NO]; 
    NSMutableArray *sortDescriptors = [NSMutableArray arrayWithObject:sortDescriptor]; 
    [_annArray sortedArrayUsingDescriptors:sortDescriptors]; 
    NSLog(@"Sorted - AnnouncementResponse%@",response); 
    */ 
    /* 
    NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"anc_id" ascending:NO]; 
    NSArray* sortedArray = [_annArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]; 
    NSLog(@"Sorted Array Response -%@",sortedArray); 
    */ 
    announcmentDone = YES; 
    if (newsDone && bulletInDone && announcmentDone && !refreshed) { 
     refreshed = YES; 
     [table reloadData]; 
    } 
} errorBlock:nil]; 

Response

+0

私の回答に応じて私にコードを提案してください.. – Fido

答えて

2

_annArrayが実際にキー "2017年9月"、 "2017年8月" などとNSDictionaryのですが...各キーの値があります1つのNSDictionaryを含む配列を私が見る限り見ることができます。あなたが期待しているようにあなたはその後、並べ替えることができるはず

NSArray *responseValues = [response[@"response"] allValues]; // An NSArray of NSArrays 
    NSMutableArray *dictionarys = [NSMutableArray new]; 
    for (NSArray *dictArrays in responseValues) { 
     for (NSDictionary *dict in dictArrays) { 
      [dictionarys addObject:dict]; 
     } 
    } 
    _annArray = dictionarys; 

:すべての値を取得するには、のようなものが必要だろう

NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"anc_id" ascending:NO]; 
    NSArray* sortedArray = [_annArray sortedArrayUsingDescriptors:@[sortDescriptor]]; 
    NSLog(@"Sorted Array Response -%@", sortedArray); 

これは、しかし、それをやっての迅速な、ラフな方法です。実際には、JSONの各ブロックをオブジェクトの型チェックとソートでオブジェクトにパースするほうがはるかに優れています。

関連する問題