Iphone 字典排序问题
我需要整理一本词典。它看起来像:Iphone 字典排序问题,iphone,sorting,nsdictionary,Iphone,Sorting,Nsdictionary,我需要整理一本词典。它看起来像: {//dictionary RU = "110.1"; //key and value SG = "150.2"; //key and value US = "50.3"; //key and value } 结果需要如下所示: {//dictionary SG = "150.2"; //key and value RU = "110.1"; //key
{//dictionary
RU = "110.1"; //key and value
SG = "150.2"; //key and value
US = "50.3"; //key and value
}
结果需要如下所示:
{//dictionary
SG = "150.2"; //key and value
RU = "110.1"; //key and value
US = "50.3"; //key and value
}
我正在尝试:
@implementation NSMutableDictionary (sorting)
-(NSMutableDictionary*)sortDictionary
{
NSArray *allKeys = [self allKeys];
NSMutableArray *allValues = [NSMutableArray array];
NSMutableArray *sortValues= [NSMutableArray array];
NSMutableArray *sortKeys= [NSMutableArray array];
for(int i=0;i<[[self allValues] count];i++)
{
[allValues addObject:[NSNumber numberWithFloat:[[[self allValues] objectAtIndex:i] floatValue]]];
}
[sortValues addObjectsFromArray:allValues];
[sortKeys addObjectsFromArray:[self allKeys]];
[sortValues sortUsingDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"floatValue" ascending:NO] autorelease]]];
for(int i=0;i<[sortValues count];i++)
{
[sortKeys replaceObjectAtIndex:i withObject:[allKeys objectAtIndex:[allValues indexOfObject:[sortValues objectAtIndex:i]]]];
[allValues replaceObjectAtIndex:[allValues indexOfObject:[sortValues objectAtIndex:i]] withObject:[NSNull null]];
}
NSLog(@"%@", sortKeys);
NSLog(@"%@", sortValues);
NSLog(@"%@", [NSMutableDictionary dictionaryWithObjects:sortValues forKeys:sortKeys]);
return [NSMutableDictionary dictionaryWithObjects:sortValues forKeys:sortKeys];
}
@end
(二)
(三)
为什么会这样?你能帮我解决这个问题吗?NSDictionary不是按顺序编写的,所以按什么顺序构建NSDictionary并不重要 有人下令戒备。如果您想在内存中对NSDictionary进行排序,您应该以某种方式创建一个键值对数组。您还可以返回两个带有相应索引的NSArray
如果您只想迭代元素的方式,您可以迭代一个已排序的键数组(这是koregan建议的)。NSDictionary不需要排序,因此构建NSDictionary的顺序无关紧要 有人下令戒备。如果您想在内存中对NSDictionary进行排序,您应该以某种方式创建一个键值对数组。您还可以返回两个带有相应索引的NSArray
如果您只想以这种方式迭代元素,那么可以迭代已排序的键数组(这是koregan建议的)。
NSDictionary
本质上是未排序的。由所有键
和所有值
检索的对象顺序始终不确定。即使您对订单进行反向工程,它也可能在下一次系统更新中更改
但是,有比所有密钥
更强大的替代方案,用于按定义和可预测的顺序检索密钥:
-用于根据值对象的keysortedbyvalueusingselector:
方法按升序排序compare:
-在iOS 4中新增,使用块进行内联排序李>keysortedbyvalueusingcomparator:
NSDictionary
本质上是不分类的。由所有键
和所有值
检索的对象顺序始终不确定。即使您对订单进行反向工程,它也可能在下一次系统更新中更改
但是,有比所有密钥
更强大的替代方案,用于按定义和可预测的顺序检索密钥:
-用于根据值对象的keysortedbyvalueusingselector:
方法按升序排序compare:
-在iOS 4中新增,使用块进行内联排序李>keysortedbyvalueusingcomparator:
- 哇。桑克斯,佩洛!这就是我需要的!我还发现了这段代码,它帮助我重新排序结果:
@implementation NSString (numericComparison) - (NSComparisonResult) floatCompare:(NSString *) other { float myValue = [self floatValue]; float otherValue = [other floatValue]; if (myValue == otherValue) return NSOrderedSame; return (myValue < otherValue ? NSOrderedAscending : NSOrderedDescending); } - (NSComparisonResult) intCompare:(NSString *) other { int myValue = [self intValue]; int otherValue = [other intValue]; if (myValue == otherValue) return NSOrderedSame; return (myValue < otherValue ? NSOrderedAscending : NSOrderedDescending); } @end
哇。桑克斯,佩洛!这就是我需要的!我还发现了这段代码,它帮助我重新排序结果:@实现NSString(数值比较) -(NSComparisonResult)floatCompare:(NSString*)其他 { 浮动myValue=[自浮动值]; 浮动其他值=[其他浮动值]; 如果(myValue==otherValue)返回OrderedName; 返回值(myValue
@implementation NSString (numericComparison) - (NSComparisonResult) floatCompare:(NSString *) other { float myValue = [self floatValue]; float otherValue = [other floatValue]; if (myValue == otherValue) return NSOrderedSame; return (myValue < otherValue ? NSOrderedAscending : NSOrderedDescending); } - (NSComparisonResult) intCompare:(NSString *) other { int myValue = [self intValue]; int otherValue = [other intValue]; if (myValue == otherValue) return NSOrderedSame; return (myValue < otherValue ? NSOrderedAscending : NSOrderedDescending); } @end
我的荣幸。别忘了接受你认为正确的答案。在搜索答案时帮助其他用户,这对我的自我有好处:)。我很高兴。别忘了接受你认为正确的答案。帮助其他用户搜索答案,对我的自我有好处:)。@实现NSString(数值比较) -(NSComparisonResult)floatCompare:(NSString*)其他 { 浮动myValue=[自浮动值]; 浮动其他值=[其他浮动值]; 如果(myValue==otherValue)返回OrderedName; 返回值(myValue
{ RU = "110.1"; SG = "150.2"; US = "50.3"; }
@implementation NSString (numericComparison) - (NSComparisonResult) floatCompare:(NSString *) other { float myValue = [self floatValue]; float otherValue = [other floatValue]; if (myValue == otherValue) return NSOrderedSame; return (myValue < otherValue ? NSOrderedAscending : NSOrderedDescending); } - (NSComparisonResult) intCompare:(NSString *) other { int myValue = [self intValue]; int otherValue = [other intValue]; if (myValue == otherValue) return NSOrderedSame; return (myValue < otherValue ? NSOrderedAscending : NSOrderedDescending); } @end