简体   繁体   English

Objective-C:按工作日对NSDate的NSMutableArray进行排序

[英]Objective-C: Sort NSMutableArray of NSDates by weekday

I have difficulties with the following task: I have a NSMutableArray containing NSManagedObjects "OpeningHours" which I pull from core data. 我在执行以下任务时遇到困难:我有一个NSMutableArray,其中包含从核心数据中提取的NSManagedObjects“ OpeningHours”。

NSMutableArray *openingHoursArray = [NSMutableArray arrayWithArray: [[museum valueForKey:@"openingHours"] allObjects]];

The array is unsorted, as getting the value for keys from my NSManagedObject "museum" returns a randomly organized set. 数组未排序,因为从我的NSManagedObject“博物馆”获取键的值会返回一个随机组织的集合。

The NSManagedObjects "openingHours" in the array have values of type NSDate, accessible via the keys "start" and "end". 数组中的NSManagedObjects“ openingHours”具有NSDate类型的值,可通过键“ start”和“ end”访问。 Now, to display my Opening Hours in a way that makes sense, I access the weekday and time information of the respective dates, that works fine so far using this piece of code: 现在,为了以一种有意义的方式显示我的开放时间,我访问了各个日期的工作日和时间信息,到目前为止,使用这段代码可以正常工作:

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:mainDelegate.localeCode];

NSDateFormatter *weekday = [[[NSDateFormatter alloc] init] autorelease];
[weekday setLocale:locale];
[weekday setDateFormat: @"EEEE"];

[NSDateFormatter *time = [[[NSDateFormatter alloc] init] autorelease];
[time setLocale:locale];
[time setTimeStyle:NSDateFormatterShortStyle];

for (NSManagedObject *openingHour in openingHoursArray) {

        NSLog(@"%@", [openingHour valueForKey:@"start"]);
        NSDate *startDate = [openingHour valueForKey:@"start"];
        NSDate *endDate = [openingHour valueForKey:@"end"];


        NSString *startDay = [weekday stringFromDate:startDate];
        NSString *endDay = [weekday stringFromDate:endDate];
        NSString *startTime = [time stringFromDate:startDate];
        NSString *endTime = [time stringFromDate:endDate];


        if (![startDay isEqualToString:endDay]) {
            [openingHoursString appendFormat:@"%@ - %@:\t%@ - %@ \n", startDay, endDay, startTime, endTime];         
        }
        else {
            [openingHoursString appendFormat:@"%@:\t%@ - %@ \n", startDay, startTime, endTime];             
        }

    }

This gives me the desired output: 这给了我想要的输出:

Thursday: 10:00 AM - 9:00 PM 星期四:10:00 AM-9:00 PM

Friday - Sunday: 10:00 AM - 6:00 PM 星期五-星期日:10:00 AM-6:00 PM

Monday - Wednesday: 10:00 AM - 5:00 PM 星期一-星期三:10:00 AM-5:00 PM

Now the problem is: of course I want the Monday opening period to be displayed first. 现在的问题是:当然,我希望星期一的开放时间首先显示。 I need to sort my array by the weekday of the start date value. 我需要按开始日期值的星期几对数组进行排序。 I tried sorting it using the following: 我尝试使用以下方法对其进行排序:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"start" ascending:TRUE];
[openingHoursArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

but obviously this doesn't work because it simply sorts by dates, and 1970-01-01 was unfortunately a thursday, therefore thursday will be the first item of my array sorted by above code. 但是显然这是行不通的,因为它只是按日期排序,而不幸的是1970-01-01是星期四,因此星期四将是我的数组中按上述代码排序的第一项。

Does anyone have a good idea on how to "sort this out"? 有人对如何“分类”有个好主意吗? :-) I'm completely stuck with this.. Thanks a lot! :-)我对此一无所知。非常感谢!

Something like this should work: 这样的事情应该起作用:

NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSArray *sortedArray = [openingHoursArray sortedArrayUsingComparator: ^(id obj1, id obj2) {
    NSDate *startDate1 = [obj1 valueForKey:@"start"];
    NSDate *startDate2 = [obj2 valueForKey:@"start"];
    NSDateComponents *components1 = [calendar components:NSWeekdayCalendarUnit fromDate:startDate1];
    NSDateComponents *components2 = [calendar components:NSWeekdayCalendarUnit fromDate:startDate2];
    NSInteger weekday1 = [components1 weekday];
    if (weekday1 == 1) weekday1 = 8; //Order sundays last
    NSInteger weekday2 = [components2 weekday];
    if (weekday2 == 1) weekday2 = 8;
    return [[NSNumber numberWithInteger:weekday1] compare:[NSNumber numberWithInteger:weekday2]];
}];

Instead of using a NSSortDescriptor, use NSArray's sortedArrayUsingSelector or NSMutableArray's sortUsingSelector methods, and pass in @selector(compare:) as the selector. 代替使用NSSortDescriptor,可以使用NSArray的sortedArrayUsingSelector或NSMutableArray的sortUsingSelector方法,并将@selector(compare:)作为选择器传递。

See this answer for more info: Click 查看此答案以获取更多信息: 单击

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

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