Ios 按dateTimeString属性排序NSMutableArray对象

Ios 按dateTimeString属性排序NSMutableArray对象,ios,sorting,nsmutablearray,Ios,Sorting,Nsmutablearray,我是一名新程序员,正在尝试使用对象对NSMutableArray进行排序,每个对象都有dateTimeString属性,我正在使用此数组在表视图上显示 代码: 对象中的属性: @property (nonatomic,strong)NSString *className; @property (nonatomic,strong)NSString *assignmentDescription; @property (nonatomic,strong)NSString *assignmentTi

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

代码:



对象中的属性:

@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进行排序

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

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

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


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

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

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

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


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

不,现在还没有排序。您至少需要尝试自己解决问题。我尝试过使用,但没有运气。不,现在还没有排序。您至少需要尝试自己解决问题。我尝试过使用,但没有运气。使用未声明标识符“assignment”那是什么?它是
AssignmentInfo
对象。现在,我已经在上面的答案中写下了完整的方法,请用它替换你的方法,应该可以很好地工作。谢谢你,它现在应该可以正常工作了!使用未声明的标识符“assignment”这是什么?它是
AssignmentInfo
对象。现在,我已经在上面的答案中写下了完整的方法,请用它替换你的方法,应该可以很好地工作。谢谢你,它现在应该可以正常工作了!
- (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;
-(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];
}