如何在iphone中排序字典数据数组中的字典数组

时间:2013-04-17 06:50:15

标签: ios nsmutabledictionary nssortdescriptor

我在字典数据数组中有字典数组,我需要根据用户选择对字典数组进行排序。

例如我的数据值是

sortResultArray--(
        {
        alertSeverity = 1;
        customerName = TPMG;
        endDate = "05-05-2013";
        isProjectHide = 1;
        kpiData =         (
                        {
                kpiActual = 18;
                kpiName = Issue;
                kpiPlanned = "";
                kpiUnit = "";
            },
                        {
                kpiActual = 7;
                kpiName = Progress;
                kpiPlanned = "";
                kpiUnit = "%";
            },
                            );
        lastInvoiceDate = "05-04-2013";
        nextBillingDate = "05-04-2013";
        nextMilestoneDate = "10-04-2013";
        nextReviewDate = "08-04-2013";
        plannedCompletionDate = "04-05-2013";
        projectID = 3000;
        projectName = "Rain Harvesting";
        projectSortType = "";
        projectStatus = 1;
        startDate = "01-01-2013";
    },
        {
        alertSeverity = 2;
        customerName = Florida;
        endDate = "12-12-2014";
        isProjectHide = 1;
        kpiData =         (
                                {
                kpiActual = 20;
                kpiName = Issue;
                kpiPlanned = "";
                kpiUnit = "";
            },
                        {
                kpiActual = 10;
                kpiName = Progress;
                kpiPlanned = "";
                kpiUnit = "%";
            },
                         );
        lastInvoiceDate = "12-05-2012";
        nextBillingDate = "12-05-2013";
        nextMilestoneDate = "16-05-2012";
        nextReviewDate = "12-05-2012";
        plannedCompletionDate = "12-08-2014";
        projectID = 4000;
        projectName = "Bridge Planning";
        projectSortType = "";
        projectStatus = 1;
        startDate = "12-12-2011";
    },
        {
        alertSeverity = 3;
        customerName = BIRO;
        endDate = "01-01-2013";
        isProjectHide = 1;
        kpiData =         (
                        {
                kpiActual = 16;
                kpiName = Issue;
                kpiPlanned = "";
                kpiUnit = "";
            },
                        {
                kpiActual = 5;
                kpiName = Progress;
                kpiPlanned = "";
                kpiUnit = "%";
            },
                           );
        lastInvoiceDate = "20-12-2013";
        nextBillingDate = "13-12-2013";
        nextMilestoneDate = "28-12-2013";
        nextReviewDate = "25-12-2013";
        plannedCompletionDate = "30-12-2013";
        projectID = 1000;
        projectName = "Rocket Launching";
        projectSortType = "";
        projectStatus = 1;
        startDate = "11-11-2012";
    },

这里假设用户选择了问题或进度键(kpiName = Issue,kpiName = Progress) ,基于kpiActual值需要排序(kpiActual = 5)。

我的意思是用户现在选择问题,排序结果是

                kpiActual = 20;
                kpiName = Issue;
and then      
                kpiActual = 18;
                kpiName = Issue;

这是我尝试过的源代码

sortResultArray = [[NSMutableArray alloc] init];
    NSMutableArray *tempArray = [[NSMutableArray alloc] init];
    [tempArray removeAllObjects];
    [tempArray addObjectsFromArray:listOfProjects];

        NSString *key = @"kpiName";
        NSSortDescriptor *statusDescriptor = [[NSSortDescriptor alloc] initWithKey:key ascending:NO];
        NSSortDescriptor *kpiDescriptor = [[NSSortDescriptor alloc] initWithKey:@"kpiActual" ascending:NO];
        NSArray *sortDescriptors = [NSArray arrayWithObjects:statusDescriptor,kpiDescriptor,nil];
        NSArray *sortedArray = [tempArray sortedArrayUsingDescriptors:sortDescriptors];
        [statusDescriptor release];
        [tempArray removeAllObjects];
        tempArray = (NSMutableArray*)sortedArray;
        [sortResultArray removeAllObjects];
        [sortResultArray addObjectsFromArray:tempArray];

0 个答案:

没有答案