1

I am a new programmer and am trying to sort an NSMutableArray with objects that each have a dateTimeString property, I am using this Array to display on a table view.

Code:

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


}

Properties in the object:

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

I need to sort by the dateTimeString

Marco
  • 6,568
  • 2
  • 25
  • 38
AbdelElrafa
  • 791
  • 6
  • 15

1 Answers1

1

As per my understanding you want to sort your Assignments based on date. Here is the sample code that you should use whenever you add any assignment.

(Typical case of Insertion Algorithm i.e. keep the array sorted whenever a new object is inserted.)

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

(Caution:I have not compiled it and also not considered putting sanity checks which you might want to, based on your usage)

Please feel free to comment in case of any issue/questions.

Ashok
  • 6,034
  • 1
  • 34
  • 50