简体   繁体   English

使用 NSDictionary 对 NSArray 进行排序

[英]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.所以, sortArray有 5 个对象,分别是星期一、星期二、星期三、星期四和星期五。 The problem is when I sort the array that needs to be sorted, let's say tempArray .问题是当我对需要排序的数组进行排序时,比如说tempArray I just created the sortArray for testing purposes of my sorting.我刚刚创建了 sortArray 以测试我的排序目的。

tempArray has NSDictionary inside it. tempArray里面有 NSDictionary。 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 .我需要像tempArray.model.title改变sortArray。 How can I do that in order to sort the nested NSArray?我该怎么做才能对嵌套的 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" }

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]];

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

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