Ios 使用多个项目对数组进行排序

Ios 使用多个项目对数组进行排序,ios,arrays,nsarray,Ios,Arrays,Nsarray,我希望对数组进行排序并对其进行操作。该数组来自服务器上的一个刮取的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

我希望对数组进行排序并对其进行操作。该数组来自服务器上的一个刮取的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)通过查看元素(例如grade)是否包含“本科”,对数组中的某些项目进行排序,并删除grade中包含该字符串的所有对象。我已经寻找这个有一段时间了,但只能在数组中有一个项目而不是多个项目时才能找到帮助


感谢您的帮助。

如果数组中的这些对象是
NSDictionary
s,那么您可以在数组中循环并实现一些简单的逻辑来获取所需的项

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) {
    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({$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})
var本科生=数组.filter({$0.grade.rangeOfString(“本科生”)!=nil})

sort({$0.grade您可以使用谓词进行筛选

NSArray *array = @[@{@"grade": @"12th"}, @{@"grade": @"12th Undergrade"}];
array = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"NOT grade LIKE %@", @"*Undergrade"]];

带块的or谓词

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

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

因此,您有一个包含两个项的数组,并且希望删除第一项,因为grade中有字符串“本科生”?您指的是筛选,而不是排序。
array = [array filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
    return [[evaluatedObject valueForKey:@"grade"] rangeOfString:@"Undergrade"].location == NSNotFound;
}]];
NSArray *filteredWithIndexSet = ({
    NSIndexSet *indexSet = [array indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
        return [[obj valueForKey:@"grade"] rangeOfString:@"Undergrade"].location == NSNotFound;
    }];
    [array objectsAtIndexes:indexSet];
});