NSMutableDictionary* filteredDictionary = [NSMutableDictionary dictionary];[filteredDictionary setobject:@"abc" forKey:@"1"];[filteredDictionary setobject:@"abc" forKey:@"3"];[filteredDictionary setobject:@"abc" forKey:@"2"];[filteredDictionary setobject:@"abc" forKey:@"5"];[filteredDictionary setobject:@"abc" forKey:@"4"];NSLog(@"%@",filteredDictionary);current output:{1 = abc;2 = abc;3 = abc;4 = abc;5 = abc; }but i want {1 = abc;3 = abc;2 = abc;5 = abc; 4 = abc;}
有没有办法按照键禁用排序?
解决方法 这是一种做法:NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"key" ascending:YES comparator:^(ID obj1,ID obj2) { if (obj1 > obj2) { return (NSComparisonResult)NSOrderedDescending; } if (obj1 < obj2) { return (NSComparisonResult)NSOrderedAscending; } return (NSComparisonResult)NSOrderedSame;}];NSArray *sortedKeys = [[filteredDictionary allKeys] sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];NSMutableDictionary *orderedDictionary = [NSMutableDictionary dictionary];for (Nsstring *index in sortedKeys) { [orderedDictionary setobject:[filteredDictionary objectForKey:index] forKey:index];}filteredDictionary = orderedDictionary;总结
以上是内存溢出为你收集整理的iphone – 如何禁用NSDictionary自动重点排序全部内容,希望文章能够帮你解决iphone – 如何禁用NSDictionary自动重点排序所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)