开发者

iPhone - Sorting NSArray based on available data in dictionary

I'm having NSDictinary objects array. Each dictionary object has keys "display_name", "first_name" and "last_name". Some dict objects have only display_name and some will not have. Some dict objects have only first_name and some will not have. Some dict objects have only last_name and some will not have.

I'm using this array to show the list in table view. What I am looking for is to sort the dict with following preference: 1. If display name is available, use that. 2. If display name is not available and first name is available, use that. 3. else last name.

How can I sort the array using above preference. I want to use NSPredicate the app has to work on older iOS as well....

I tried different combinations of NSPredicate as following, but I didn't succeeed:

    NSSortDescriptor* firstNameDescriptor;
    NSSortDescriptor* lastNameDescriptor;
    NSSortDescriptor* displayNameDescriptor;
    displayNameDescr开发者_如何学JAVAiptor = [NSSortDescriptor sortDescriptorWithKey:@"display_name" ascending:YES];
    lastNameDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"last_name" ascending:YES];                      
    firstNameDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"first_name" ascending:YES];

    NSArray *sortDescriptors = [NSArray arrayWithObjects:firstNameDescriptor, lastNameDescriptor,nil];

    self.contactsArray = (NSMutableArray*)[tempArray sortedArrayUsingDescriptors:sortDescriptors];

Can some one guide me in right way to achieve it?


you can use :

sortedArrayUsingFunction:context:

and implement the rules you just listed in your own custom sorting function


What I did is that during sorting, I added another key to the dictionary "final_name" and the value is set according to my preference of names to display and just sorted the array with "final_name".

    NSArray* tempArray = [jsonData objectForKey:@"contacts"];
    for (NSDictionary* conDict in tempArray) 
    {
        NSString* fName = [conDict objectForKey:@"first_name"];
        NSString* lName = [conDict objectForKey:@"last_name"];
        NSString* dName = [conDict objectForKey:@"display_name"];

        NSString* finalName = @"<<No Name>>";

        if (dName && ![dName isEqual:[NSNull null]]) {
            finalName = dName;
        }
        else if (fName && ![fName isEqual:[NSNull null]] && lName && ![lName isEqual:[NSNull null]]) 
        {
            finalName = [NSString stringWithFormat:@"%@ %@",fName,lName];
        }
        else if (fName && ![fName isEqual:[NSNull null]]) 
        {
            finalName = fName;
        }
        else if (lName && ![lName isEqual:[NSNull null]]) {
            finalName = lName;
        }
        [conDict setValue:finalName forKey:@"final_name"];
    }

    if ([tempArray count]) 
    {
        NSSortDescriptor* finalSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"final_name" ascending:YES];
        NSArray *sortDescriptors = [NSArray arrayWithObjects:finalSortDescriptor,nil];
        self.contactsArray = [[NSArray alloc] initWithArray:[tempArray sortedArrayUsingDescriptors:sortDescriptors]];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜