对其中包含 NSDictionary 的 NSArray 进行排序
Sorting a NSArray with NSDictionary inside it
我需要从 星期一到星期五 按顺序对我的数组进行排序。我在这里做了那部分
NSArray *userDays = [NSArray arrayWithObjects:@"Monday", @"Tuesday", @"Wednesday",@"Thursday",@"Friday",nil];
[sortArray addObject:model.title];
NSArray *sortedUserDays = [userDays filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF in (%@)", sortArray]];
因此,sortArray 有 5 个对象,分别是星期一、星期二、星期三、星期四和星期五。问题是当我对需要排序的数组进行排序时,比方说 tempArray。我刚刚创建了 sortArray 用于测试我的排序目的。
tempArray 里面有 NSDictionary。日期(星期一、星期二等...)在关键字 title.
中
现在在这一行
NSArray *sortedUserDays = [userDays filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF in (%@)", sortArray]];
我需要将 sortArray 更改为 tempArray.model.title。我该怎么做才能对嵌套的 NSArray 进行排序?
示例数据:
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" }
预期输出:
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" }
我不确定我是否理解您要查找的内容,但可能是这样的吗?
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]];
我需要从 星期一到星期五 按顺序对我的数组进行排序。我在这里做了那部分
NSArray *userDays = [NSArray arrayWithObjects:@"Monday", @"Tuesday", @"Wednesday",@"Thursday",@"Friday",nil];
[sortArray addObject:model.title];
NSArray *sortedUserDays = [userDays filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF in (%@)", sortArray]];
因此,sortArray 有 5 个对象,分别是星期一、星期二、星期三、星期四和星期五。问题是当我对需要排序的数组进行排序时,比方说 tempArray。我刚刚创建了 sortArray 用于测试我的排序目的。
tempArray 里面有 NSDictionary。日期(星期一、星期二等...)在关键字 title.
中现在在这一行
NSArray *sortedUserDays = [userDays filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF in (%@)", sortArray]];
我需要将 sortArray 更改为 tempArray.model.title。我该怎么做才能对嵌套的 NSArray 进行排序?
示例数据:
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" }
预期输出:
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" }
我不确定我是否理解您要查找的内容,但可能是这样的吗?
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]];