0

My objects have a timestamp and a text, the timestamp is a NSDate formatted to a NSString before saving. After creating a new object and setting its values I save it. Later when loading it I put all objects into an Array and mix that Array with another Array, thats why I need to sort the Array.
My problem is: I don't know how to properly sort the Array by the object's timestamp property.
I have been searching a lot but still don't have an up-to-date-answer. Any help is appreciated.

user2414460
  • 211
  • 3
  • 14
  • Use `sortedArrayUsingComparator:`. In the comparator block, convert the NSString timestamps to NSDate objects and `compare:` one to the other. – neilco Dec 05 '13 at 10:37

3 Answers3

2

To compare two different time stamp you have to convert it to NSData first:

-(NSDate)convertToDate:(NSString*)inputDate
    //NSString *inputDate = @"11/20/2013 3:30:05 PM"; //<-- Your date probably is different than that
    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
    dateFormatter1.dateFormat = @"MM/dd/yyyy hh:mm:ss a"; //<-- this needs to be changet to match your time stamp format
    [dateFormatter1 setLocale:[NSLocale currentLocale]];
    NSDate *date = [dateFormatter1 dateFromString:inputDate]; //<- this is your NSDate object.
    return date;
}



NSArray *sortedArray = [unSortedArray  sortedArrayUsingComparator: ^NSComparisonResult(DateObj *id1, DateObj *id2) {
            NSDate d1 = [self convertToDate:id1.timeStamp];
            NSDate d2 = [self convertToDate:id2.timeStamp];
            return [d1 compare:d2];
        }];

You can also add compare method to your class, for example:

- (NSComparisonResult)compare:(YourClass *)obj
{
    NSDate d1 = [self convertToDate:self.timeStamp];
    NSDate d2 = [self convertToDate:obj.timeStamp];
    return [d1 compare:d2];
}

And if you need to sort array of this object you call:

NSArray *sortedArray = [yourArray sortedArrayUsingSelector:@selector(compare:)];
Greg
  • 25,317
  • 6
  • 53
  • 62
1

Try this;

NSArray *sortedArray = [unSortedArray  sortedArrayUsingComparator: ^NSComparisonResult(DateObj *id1, DateObj *id2) {
            if(id1.timeStamp > id2.timeStamp)
                return NSOrderedAscending;
            else if (id1.timeStamp < id2.timeStamp)
                return NSOrderedDescending;
            else return NSOrderedSame;
        }];

You will get more about sorting HERE.

Community
  • 1
  • 1
KethanKumar
  • 716
  • 5
  • 17
0

My solution:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd.MM.YYYY HH:mm:ss"];
NSMutableArray *timeStamps = [[NSMutableArray alloc] init];
for(Object *object in allObjects) {
    NSDate *time = [dateFormatter dateFromString:object.timeStamp];
    [timeStamps addObject:time];
}

[timeStamps sortedArrayUsingSelector:@selector(compare:)];
NSMutableArray *sortedArray = [NSMutableArray arrayWithCapacity:[allObjects count]];

for(int i = 0; i < [timeStamps count]; i++) {
   for(Object *object in allObjects) {
        if(object.timeStamp == [dateFormatter stringFromDate:timeStamps[i]]) {
            [sortedArray addObject:messageObject];
        }
    }
}
return sortedArray;
user2414460
  • 211
  • 3
  • 14