我希望对数组进行排序并对其进行操作。该数组来自服务器上的已删除XML文件,如下所示:
{
award = Varies;
deadline = "3/1";
description = "To be considered for many Northeastern scholarships, you must apply for admission, submit a high school transcript, and complete the necessary scholarship applications NO LATER than March 1st prior to your anticipated fall enrollment. Additional scholarships requiring a separate application, portfolio, personal interview, or tryout are also available.";
gpa = "2.50";
grade = "12TH Undergraduate";
link = "http://www.njc.edu/Scholarships/";
location = Colorado;
title = "Northeastern Junior College Scholarships";
},
{
award = "$1,000";
deadline = "3/1";
description = "Must be a resident of the State of Colorado and a citizen of the United States. Criteria for selection include: a short written essay on the Mayflower on the topic listed in the application, GPA and class rank, ACT or SAT scores, evidence of honors received, activities in and outside of high school, employment and leadership, a letter of recommendation.";
gpa = "0.00";
grade = 12TH;
link = "http://www.coloradomayflowersociety.org/scholarship.htm";
location = Colorado;
title = "Colorado Mayflower Society: Scholarship";
},
我期待1)通过查看元素(例如成绩)是否包含"本科生"来排序数组中的某些项目。并删除成绩中包含该字符串的所有对象。我一直在寻找这个,但只有在数组中有一个项目时才能找到帮助,而不是多个。
感谢您的帮助。
答案 0 :(得分:1)
有很多方法......例如(对于可变数组):
NSIndexSet *removeIndexes = [array indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return obj[@"grade"] && [(NSString *)obj[@"grade"] rangeOfString:@"Undergraduate"].location != NSNotFound;
}];
if (removeIndexes.count) {
[array removeObjectsAtIndexes:removeIndexes];
}
编辑:如果您需要检查所有值:
NSIndexSet *removeIndexes = [array indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
NSUInteger index = [[(NSDictionary *)obj allValues] indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return [obj isKindOfClass:[NSString class]] && [(NSString *)obj rangeOfString:@"Undergraduate"].location != NSNotFound;
}];
return index != NSNotFound;
}];
答案 1 :(得分:0)
使用过滤器将阵列拆分为两个阵列。一个是毕业生,另一个是本科生。然后使用sort对它们进行排序。喜欢这个
var undergraduates = array.filter({$0.grade.rangeOfString("Undergraduate") != nil})
undergraduates.sort({$0.grade <= $1.grade})
var graduates = array.filter({$0.grade.rangeOfString("Undergraduate") == nil})
graduates.sort({$0.grade <= $1.grade})
答案 2 :(得分:0)
您可以使用谓词进行过滤
NSArray *array = @[@{@"grade": @"12th"}, @{@"grade": @"12th Undergrade"}];
array = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"NOT grade LIKE %@", @"*Undergrade"]];
或
array = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"NOT grade CONTAINS[cd] %@", @"Undergrade"]];
或带有块的谓词
array = [array filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
return [[evaluatedObject valueForKey:@"grade"] rangeOfString:@"Undergrade"].location == NSNotFound;
}]];
NSArray还提供索引过滤
NSArray *filteredWithIndexSet = ({
NSIndexSet *indexSet = [array indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return [[obj valueForKey:@"grade"] rangeOfString:@"Undergrade"].location == NSNotFound;
}];
[array objectsAtIndexes:indexSet];
});
注意:此示例使用语句表达式语法。
答案 3 :(得分:-1)
如果数组中的这些对象是NSDictionary
,那么你可以遍历数组并实现一些简单的逻辑来获得你想要的项目。
NSMutableArray *filteredArray = [[NSMutableArray alloc] initWithCapacity:array.count];
for (NSDictionary *dict in array) {
if (![dict[@"grade"] containsString:@"Undergraduate") {
[filteredArray addObject:dict];
}
}