Search code examples
iosobjective-csortingnsarraynsdictionary

Sorting a NSArray with NSDictionary inside it


I need to sort my array from Monday to Friday sequentially. I did that part here

NSArray *userDays = [NSArray arrayWithObjects:@"Monday", @"Tuesday", @"Wednesday",@"Thursday",@"Friday",nil];
[sortArray addObject:model.title];
NSArray *sortedUserDays = [userDays filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF in (%@)", sortArray]];

So, sortArray has 5 objects which is Monday, Tuesday, Wednesday, Thursday and Friday. The problem is when I sort the array that needs to be sorted, let's say tempArray. I just created the sortArray for testing purposes of my sorting.

tempArray has NSDictionary inside it. And the days (monday,tuesday,etc...) are in the key title.

Now in this line

NSArray *sortedUserDays = [userDays filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF in (%@)", sortArray]];

I need to change sortArray by something like tempArray.model.title. How can I do that in order to sort the nested NSArray?

Example data:

tempArray[0]
{ title : "Friday", identity: "someUniqueIdentity", url: "httpwww.123" }

tempArray[1]
{ title: "Wednesday", identity: "someUniqueIdentity2", url :"httpwww.1233" }

tempArray[2]
{ title: "Monday", identity: "someUniqueIdentity3", url: "httpwww.1233" }


tempArray[3]
{ title: "Thursday", identity: "someUniqueIdentity3", url :"httpwww.1233" }


tempArray[4]
{ title: "Tuesday", identity: "someUniqueIdentity3", url: "httpwww.1233" }

Expected output:

tempArray[0]
{ title: "Monday", identity: "someUniqueIdentity3", url: "httpwww.1233" }

tempArray[1]
{ title: "Tuesday", identity: "someUniqueIdentity3", url :"httpwww.1233" }

tempArray[2]
{ title: "Wednesday", identity: "someUniqueIdentity2", url :"httpwww.1233" }

tempArray[3]
{ title: "Thursday", identity: "someUniqueIdentity3", url :"httpwww.1233" }

tempArray[4]
{ title: "Friday", identity: "someUniqueIdentity", url: "httpwww.123" }

Solution

  • I am not sure that I understand what you are looking for, but could it be something like this?

    NSArray *tempArray = @[
            @{ @"title": @"Friday", @"identity" : @"someUniqueIdentity", @"url" : @"httpwww.123"},
            @{ @"title" : @"Wednesday", @"identity" :  @"someUniqueIdentity2", @"url" :  @"httpwww.1233" },
            @{ @"title" : @"Monday", @"identity" :  @"someUniqueIdentity3", @"url" :  @"httpwww.1233" },
            @{ @"title" : @"Thursday", @"identity" :  @"someUniqueIdentity3", @"url" :  @"httpwww.1233" },
            @{ @"title" : @"Tuesday", @"identity" :  @"someUniqueIdentity3", @"url" :  @"httpwww.1233" }
    ];
    
    NSArray *weekdays = @[@"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday"];
    
    NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES comparator:^NSComparisonResult(NSString *day1, NSString *day2) {
        NSUInteger index1 = [weekdays indexOfObject:day1];
        NSUInteger index2 = [weekdays indexOfObject:day2];
        return (NSComparisonResult) (index1 >= index2);
    }];
    
    NSArray *result = [tempArray sortedArrayUsingDescriptors:@[descriptor]];