按dateTimeString属性对NSMutableArray对象进行排序

时间:2013-10-23 01:47:53

标签: ios sorting nsmutablearray

我是一名新程序员,我正在尝试使用每个都有dateTimeString属性的对象对NSMutableArray进行排序,我使用此数组在表视图上显示。

代码:

-(void)newAssignment:(AssignmentInfo *)assignment
{
    [self.alist addObject:assignment];
    NSString *filePath = [self dataFilePath];
    [NSKeyedArchiver archiveRootObject:self.alist toFile:filePath];
    [self.tableView reloadData];

}

- (IBAction)addTheInfo:(id)sender {

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
    dateFormatter.timeStyle = NSDateFormatterShortStyle;
    dateFormatter.dateStyle = NSDateFormatterShortStyle;
    NSString *dateTimeString = [dateFormatter stringFromDate: dateTimePicker.date];
    self.assignmentInfo.className = self.className.text;
    self.assignmentInfo.assignmentTitle = self.assignmentTitle.text;
    self.assignmentInfo.assignmentDescription = self.assignmentDescription.text;
    self.assignmentInfo.dateTimeString = dateTimeString;

    [self presentMessage:self.assignmentInfo.description];
    [self dismissViewControllerAnimated:YES completion:nil];


    [self.otherdelegate newAssignment:self.assignmentInfo];
    NSLog(@"%@",self.otherdelegate);

    if (self.edit) {
        [self.otherdelegate deletedAssignment:self.assignmentEditing];
        }


}

对象中的属性:

@property (nonatomic,strong)NSString *className;
@property (nonatomic,strong)NSString *assignmentDescription;
@property (nonatomic,strong)NSString *assignmentTitle;
@property (nonatomic,strong)NSString *dateTimeString;
@property (nonatomic)bool notifcationStatus;

我需要按dateTimeString

排序

1 个答案:

答案 0 :(得分:1)

根据我的理解,您希望根据日期对作业进行排序。以下是添加任何作业时应使用的示例代码。

(插入算法的典型情况,即每当插入新对象时保持数组排序。)

-(void)newAssignment:(AssignmentInfo *)assignment

            NSUInteger rowIndex = [self.alist indexOfObject:assignment inSortedRange:NSMakeRange(0, self.alist.count) options:NSBinarySearchingInsertionIndex usingComparator:^(AssignmentInfo *obj1, AssignmentInfo *obj2) {

                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
                dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
                dateFormatter.timeStyle = NSDateFormatterShortStyle;
                dateFormatter.dateStyle = NSDateFormatterShortStyle;

                NSDate *date1 = [dateFormatter dateFromString:obj1.dateTimeString];
                NSDate *date2 = [dateFormatter dateFromString:obj2.dateTimeString];

                if ([date1 compare:date2] == NSOrderedAscending) {
                    return NSOrderedAscending;
                }
                if ([date1 compare:date2] == NSOrderedDescending) {
                    return NSOrderedDescending;
                }
                return NSOrderedSame;
            }];

            [self.alist insertObject:assignment atIndex:rowIndex];

            NSString *filePath = [self dataFilePath];
            [NSKeyedArchiver archiveRootObject:self.alist toFile:filePath];
            [self.tableView reloadData];
}

警告:我没有编译它,也没考虑根据您的使用情况进行您可能需要的健全性检查)

如有任何问题,请随时发表评论。