NSDictionary里的keysSortedByValueUsingSelector排序使用

NSDictionary里的keysSortedByValueUsingSelector排序使用

@implementation NSString(compare)
  
-(NSComparisonResult)floatCompare:(NSString*)other
{

float myValue = [self floatValue];
float otherValue= [other floatValue];
if(myValue == otherValue) return NSOrderedSame;
return (myValue < otherValue ? NSOrderedAscending : NSOrderedDescending);
}

@end


NSString* names[] = ", @"220091111", @"5", @"7",nil};
NSString* values[] = ", @"23.5", @"89.12", @"333.2", nil};
NSDictionary* dic = [NSDictionary dictionaryWithObjects:(id*)names forKeys:(id*)values count:4];
NSArray* res = [dic keysSortedByValueUsingSelector:@selector(floatCompare:)];
NSLog(@"%@", res);
NSLog(@"%@", dic);

你可能感兴趣的:(IOS)