2016-11-25 68 views
0
Student *s1 = [Student new]; 
s1.city = @"Delhi"; 

Student *s2 = [Student new]; 
s2.city = @"Mumbai"; 

NSArray *arrModels = @[s1,s2]; 
NSArray *arrCompareModel = @[@"Mumbai",@"Delhi"]; 

我需要根据on arrCompareModelarrModels进行排序。基于比较模型对阵列进行排序iOS

web中的所有解决方案均与升序相关。但在这里我有一个自定义模型。 我该如何实现它?

+1

使用'sortedArrayUsingComparator:',你可以检查http://stackoverflow.com/questions/38768463/sort-nsarray-custom-objects-by-another-nsarray-custom-objects/38769228#38769228与'indexforItem:inList:'可能只是'[arrCompareModel indexOfObject:student1Or2AccordingToTheCase.city];' – Larme

+0

检查我的答案 –

回答

0

你需要写这个排序降序

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"city" ascending:false]; 
NSArray *sortedArray = [arrModels sortedArrayUsingDescriptors:@[sortDescriptor]]; 
+0

这不是问题灰。他希望根据'arrCompareModel'的顺序排列物品。 – Larme

+0

,这是没有像OP寻找。 – holex

0

尝试类似的东西:

NSArray *sortedArray = [arrModels sortedArrayUsingComparator:^NSComparisonResult(Student *obj1, Student *obj2) { 
    return [arrCompareModel indexOfObject:obj1.city] < [arrCompareModel indexOfObject:obj2.city]; 
}];