iphone – 如何禁用NSDictionary自动重点排序

iphone – 如何禁用NSDictionary自动重点排序,第1张

概述当我在NSMutableDictionary中添加值时,它将自动设置Key.我如何禁用它,并按照第一组和第二组进行排列. NSMutableDictionary* filteredDictionary = [NSMutableDictionary dictionary];[filteredDictionary setObject:@"abc" forKey:@"1"];[filteredDi 当我在NSMutableDictionary中添加值时,它将自动设置Key.我如何禁用它,并按照第一组和第二组进行排列.

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自动重点排序所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1083638.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存