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.

1
  • Use sortedArrayUsingComparator:. In the comparator block, convert the NSString timestamps to NSDate objects and compare: one to the other. Commented Dec 5, 2013 at 10:37

3 Answers 3

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:)];
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

BOOL (which has two possible values) is not a synonym for NSComparisonResult (which has three possible values).
Use loop to compare more than two values.
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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.