Search code examples
iosobjective-cuicollectionviewnsarray

Sort UICollectionView by Date


My app includes a plist file that is my data for a UICollectionView. The plist looks like this: enter image description here

Here is my code for the collection view:

-(NSArray *)content {
    if (!_content) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                             NSUserDomainMask,
                                                             YES);
        NSString *plistPath = [[paths lastObject] stringByAppendingPathComponent:@"myJournal.plist"];
        NSDictionary *help = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
        _content = [help allValues];
        _titles = [help allKeys];
        NSLog(@"CONTENT%@", _content);
    }
    return _content;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
       return [self.content count];
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {


    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"MM/dd/yyyy";
    NSDate *date = [dateFormatter dateFromString:[self.titles objectAtIndex:indexPath.row]];

    dateFormatter.dateFormat=@"MMMM";
    NSString *monthString = [[dateFormatter stringFromDate:date] capitalizedString];

    dateFormatter.dateFormat=@"dd";
    NSString *dayString = [dateFormatter stringFromDate:date];
    static NSString *cellIdentifier = @"cvCell";

    CVCell *cell = (CVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];


    [cell.labels setText:[[self.content objectAtIndex:indexPath.row] valueForKey:@"verse"]];
    [cell.month setText:monthString];
    [cell.day setText:dayString];

    return cell;
}

Here is how the view looks: As you can tell, it's not QUITE the way I would like it to lay out. I would prefer either in order from left to right and top to bottom, or newest entries first, but I can't QUITE get it. Any suggestions on how I can accomplish this?

UPDATE:

I have added an NSArray and use that to get the data from plist first, and then use an NSSortDescriptor to get the _content array created. In the NSLog, it shows it in the order I want, but the end result doesn't change:

-(NSArray *)content {
    if (!_content) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                             NSUserDomainMask,
                                                             YES);
        NSString *plistPath = [[paths lastObject] stringByAppendingPathComponent:@"myJournal.plist"];
        NSDictionary *help = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
        _sortedArray = [help allValues];
        _titles = [help allKeys];


        NSSortDescriptor* nameSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"theDate" ascending:NO];
        _content = [_sortedArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:nameSortDescriptor]];
        NSLog(@"%@", _content);
    }
    return _content;
}

enter image description here


Solution

  • There are two ways of doing this. One is to setup your plist to contain an array instead of a dictionary at the root level (see screenshot). This makes sure that you preserve the order of the items (a dictionary has no order).

    enter image description here

    Another way is to order the items based on the key. You can do this as follow:

    -(NSArray *)content {
        if (!_content) {
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                                 NSUserDomainMask,
                                                                 YES);
            NSString *plistPath = [[paths lastObject] stringByAppendingPathComponent:@"myJournal.plist"];
            NSDictionary *help = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
            _titles = [help allKeys];
    
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            dateFormatter.dateFormat = @"MM/dd/yyyy";
    
            NSArray *sortedTitles = [_titles sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) {
    
                NSDate *date1 = [dateFormatter dateFromString:obj1];
                NSDate *date2 = [dateFormatter dateFromString:obj2];
                return [date1 compare:date2];
            }];
    
            _sortedArray = [[NSMutableArray alloc] init];
    
            for (NSString *title in sortedTitles) {
                [_sortedArray addObject:[help objectForKey:title]];
            }
            _content = _sortedArray;
            NSLog(@"%@", _content);
        }
        return _content;
    }