简体   繁体   English

根据NSString日期对对象进行排序

[英]Sort objects based on a NSString date

I have an array of objects which all contain a expectedDeliveryDate string. 我有一个对象数组,每个对象都包含一个expectedDeliveryDate字符串。 I have a method which takes a date string and turns it into a NSDate . 我有一个采用日期字符串并将其转换为NSDate

How can I sort these in ascending or descending order? 如何将它们按升序或降序排序?

I am assuming that you have an Array in which each object is Dictionary and it contains data of delivery 我假设您有一个Array,其中每个对象都是Dictionary,并且其中包含传递数据

try this I create this method in my project to sort array with Key. 试试这个,我在我的项目中创建此方法,以使用Key对数组进行排序。

In this method lastDescriptor has selector compare: so two date will compare and sort automatically in this method. 在此方法中,lastDescriptor具有选择器compare:因此,两个日期将在此方法中进行比较和自动排序。

-(NSMutableArray *)sortArrayWithKey:(NSString *)sortingKey andArray:(NSMutableArray *)unsortedArray{
    NSSortDescriptor *lastDescriptor = [[NSSortDescriptor alloc] initWithKey:sortingKey ascending:NO selector:@selector(compare:)];
    NSArray *descriptors = [NSArray arrayWithObjects: lastDescriptor, nil];
    return [unsortedArray sortedArrayUsingDescriptors:descriptors].mutableCopy;
}

Hope this help you too.. 希望这对您也有帮助。

+ (void)filterAlertsByTime:(NSMutableArray*)alerts {


NSDateFormatter *hourFormatter = [[NSDateFormatter alloc] init];
[hourFormatter setDateFormat:@"MM/dd/yyyy hh:mm a"];
[alerts sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    NSDate *date1 = [hourFormatter dateFromString:[NSString stringWithFormat:@"%@",obj1[@"expectedDeliveryDate"]]];
    NSDate *date2 =[hourFormatter dateFromString:[NSString stringWithFormat:@"%@",obj2[@"expectedDeliveryDate"]]] ;


    return [date1 compare:date2];

}];
}

Please edit the date format according to the format of the expectedDeliveryDate string 请根据ExpectedDeliveryDate字符串的格式编辑日期格式

Sorting the array with NSString date. 使用NSString日期对数组进行排序。

Things Done: 完成的事情:

  1. Convert the array which has NSString Date to NSDate 将具有NSString Date的数组转换为NSDate
  2. Then sort the array with NSDate. 然后使用NSDate对数组进行排序。

Try this 尝试这个

NSMutableArray *oldArray; //Array which consist of object with date as string.
NSMutableArray *newArray = [[NSMutableArray alloc] init];
if(oldArray.count!=0){
    //CONVERT STRING TO DATE
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MMM d, yyyy"];
    [dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
    for (int i=0 ;i<oldArray.count;i++)
    {
      NSMutableDictionary *dict= [oldArray objectAtIndex:i];            
        NSString *stringDate = [dict objectForKey:@"stringDateKey"];
        if(![stringDate isEqualToString:@""]){


            NSDate *date = [dateFormat dateFromString:stringDate];
            NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
            [newDict addEntriesFromDictionary:dict];
            [newDict setObject:date forKey:@"newDate"];                
            [newArray addObject:newDict];
        }
        else{                
            [newArray addObject:dict];
        }
    }
    //SORTING THE ARRAY
    NSSortDescriptor *firstDescriptor = [[NSSortDescriptor alloc] initWithKey:@"newDate" ascending:NO];
    NSArray *sortDescriptors = [NSArray arrayWithObjects:firstDescriptor, nil];
    NSArray *sortedArray = [dataValue sortedArrayUsingDescriptors:sortDescriptors];

}

You can use sort descriptors to sort objects by their dates. 您可以使用排序描述符按对象的日期对其进行排序。 So first convert your strings to NSDates and set them on another property of your custom object, and sort them that way. 因此,首先将字符串转换为NSDates并将其设置在自定义对象的另一个属性上,然后以这种方式对其进行排序。

 NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"expectedDeliveryDate" 
                                                              ascending:YES];
 NSArray *sortedDeliverDates = [deliveryItems sortedArrayUsingDescriptors:@[descriptor]];

Furthermore NSDate also has a compare: method that will let you compare two dates. 此外,NSDate还具有compare:方法,该方法可让您比较两个日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM