[英]sort mixed array with objects
My objects have a timestamp and a text, the timestamp is a NSDate formatted to a NSString before saving. 我的对象有时间戳和文本,时间戳是在保存之前格式化为NSString的NSDate。 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. 我的问题是:我不知道如何通过对象的timestamp属性正确地对Array进行排序。
I have been searching a lot but still don't have an up-to-date-answer. 我一直在搜索,但仍然没有最新的答案。 Any help is appreciated. 任何帮助表示赞赏。
To compare two different time stamp you have to convert it to NSData first: 要比较两个不同的时间戳,您必须先将其转换为NSData:
-(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:)];
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;
}];
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.