已经有很多问题要求对NSMutableArray进行排序仍然无法找到我问题的确切解决方案。请跟进:
我有 arrMasterArray ,它是这样的:
//This is an array of dictionaries with each element having dictionary at its index and key-value for the same.
<__NSArrayM 0x8bbaca0>(
{
"Brand_Description" = Accolate;
"Medication_DateStart" = "5/5/2012 12:00:00 AM";
},{
"Brand_Description" =Trisenox;
"Medication_DateStart" = "5/5/2012 12:00:00 AM";
}, {
"Brand_Description" = Ultiva;
"Medication_DateStart" = "5/5/2012 12:00:00 AM";
} ,{
"Brand_Description" = Accretropin;
"Medication_DateStart" = "5/5/2012 12:00:00 AM";
}
我在 arrMasterArray 上应用排序,它返回一个排序数组,但只包含该特定keyPath的元素:
NSArray *arrNameCompare= [[arrMasterArray valueForKeyPath:@"Brand_Description"] sortedArrayUsingSelector:@selector(compare:)];
输出如下内容:
<__NSArrayI 0x10e0efb0>( Accolate, Accretropin, Trisenox, Ultiva )
现在我希望 arrMasterArray 根据**arrNameCompare**
进行排序。
我搜索了很多但没有运气。感谢您的耐心和对不起我的英语。 再次感谢。
答案 0 :(得分:2)
答案 1 :(得分:1)
我认为您正在寻找的是NSMutableArray方法,sortUsingDescriptors:。此方法将“(可变)”数组“就地”排序,而不是像sortedArrayUsingSelector:创建一个新数组。所以,如果我理解你要做什么,那么这些行将根据Brand_Description的值对arrMasterArray进行排序:
NSSortDescriptor *sorter = [NSSortDescriptor sortDescriptorWithKey:@"Brand_Description" ascending:YES];
[arrMasterArray sortUsingDescriptors:[NSArray arrayWithObject:sorter]];