2014-05-03 47 views
0

我有一個名爲Expense的實體的核心數據模型。它包含一個名爲time的屬性,它存儲一個NSDate。將核心數據過濾/排序到陣列

將所有這些費用排序爲按月分類的單個NSMutableArray的有效方法是什麼?

例如,如果有五個費用與日期如下:

1 2014年1月6日

2 2014年2月4日

3 2014年5月2日

4 2014年6月5日

5 2014年9月4日

一個NSMutableArray應被返回的是一堆數組。每個數組代表1個月,其中包含歸因於它的所有實體。

+0

使用看看這個蘋果樣品project.https://developer.apple.com/library/ios/samplecode/DateSectionTitles /Introduction/Intro.html#//apple_ref/doc/uid/DTS40009939 ref。 - http://stackoverflow.com/a/5610898/1547907 –

回答

1

實施例:

// Get expenses from Core Data 
NSArray *expensesToCategorize = ...; 

// Create calendar 
static NSCalendar *gregorian = nil; 
static dispatch_once_t onceToken; 
dispatch_once(&onceToken, ^{ 
    gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
}); 

// Set time zone of calendar 
[gregorian setTimeZone:[NSTimeZone defaultTimeZone]]; 

// Group expenses by month 
NSMutableDictionary *expensesByMonth = [[NSMutableDictionary alloc] init]; 
for (Expense *expense in expensesToCategorize) { 
    NSDate *date = expense.time; 

    NSDateComponents *components = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:date]; 
    [components setDay:1]; 

    NSDate *firstDayOfMonth = [gregorian dateFromComponents:components]; 

    NSMutableArray *expensesThisMonth = expensesByMonth[firstDayOfMonth]; 
    if (!expensesThisMonth) { 
     expensesThisMonth = [[NSMutableArray alloc] init]; 
     expensesByMonth[firstDayOfMonth] = expensesThisMonth; 
    } 

    [expensesThisMonth addObject:expense]; 
} 

// Transform into desired data structure 
NSArray *sortedMonthDates = [[expensesByMonth allKeys] sortedArrayUsingSelector:@selector(compare:)]; 
NSArray *categorizedExpenses = [expensesByMonth objectsForKeys:sortedMonthDates notFoundMarker:[NSNull null]]; 

// Sort expenses within each month by date 
for (NSMutableArray *expensesThisMonth in categorizedExpenses) { 
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"time" ascending:YES]; 
    [expensesThisMonth sortUsingDescriptors:@[sortDescriptor]]; 
} 
1

陣列自定義對象

NSArray *sorted = [arr sortedArrayUsingFunction:dateSort context:nil]; 

排序功能

NSComparisonResult dateSort(Class *s1, Class *s2, void *context) { 
NSDate *d1 = [Utility getDateFromTheString:s1.strDate]; 
NSDate *d2 = [Utility getDateFromTheString:s2.strDate]; 
return -1*[d1 compare:d2]; 

}

字符串轉換成日期

+(NSDate *)getDateFromTheString:(NSString *)strDate{ 
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]]; 
    [dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]]; 
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
    NSDate *date = [dateFormat dateFromString:strDate]; 
    return date; 
} 
0

您是否在尋找fetchedResultsController?

例如下面的代碼填充一個fetchedResultsController用於在分組TableViewController

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Location" 
               inManagedObjectContext:_managedObjectContext]; 
    [fetchRequest setEntity:entity]; 

    [fetchRequest setSortDescriptors:@[ 
    [[NSSortDescriptor alloc] initWithKey:@"map_name" ascending:YES], 
    [[NSSortDescriptor alloc] initWithKey:@"order_num" ascending:YES] 
    ]]; 

    [fetchRequest setFetchBatchSize:20]; 


    // Use the sectionIdentifier property to group into sections. 

    _fetchedResultsController = 
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
             managedObjectContext:_managedObjectContext 
              sectionNameKeyPath:@"map_name" 
                cacheName:@"LocationsList"];