对包含多个项目的数组进行排序

Sort Array with multiple items

我想对数组进行排序并对其进行操作。该数组来自服务器上的一个抓取的 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 = ",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) 通过查看元素(例如成绩)是否包含 "undergraduate" 对数组中的某些项目进行排序,并删除成绩中包含该字符串的所有对象。我一直在寻找这个,但只有当数组中有一个项目而不是多个项目时才能找到帮助。

感谢您的帮助。

如果数组中的这些对象是 NSDictionary,那么您可以遍历数组并实现一些简单的逻辑来获取您想要的项目。

NSMutableArray *filteredArray = [[NSMutableArray alloc] initWithCapacity:array.count];
for (NSDictionary *dict in array) {
    if (![dict[@"grade"] containsString:@"Undergraduate") {
        [filteredArray addObject:dict];
    }
}

有很多方法...例如(对于可变数组):

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

使用过滤器将数组拆分为两个数组。一个给研究生,另一个给本科生。然后使用 sort 对它们进行排序。像这样

    var undergraduates = array.filter({[=10=].grade.rangeOfString("Undergraduate") != nil})
    undergraduates.sort({[=10=].grade <= .grade})

    var graduates = array.filter({[=10=].grade.rangeOfString("Undergraduate") == nil})
    graduates.sort({[=10=].grade <= .grade})

您可以使用谓词进行过滤

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

注:本例使用语句表达式语法。