1

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

代码:

-(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 排序

4

1 回答 1

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

注意:我没有编译它,也没有考虑根据您的使用情况进行您可能想要的完整性检查)

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

于 2013-10-23T02:10:04.140 回答