NSDictionary里排序方法keysSortedByValueUsingSelector的使用

@implementation NSString(compare)

-(NSComparisonResult)intCompare:(NSString *)other
{
    int myValue = [self floatValue];
    int otherValue= [other floatValue];
    
    if (myValue == otherValue) {
        return NSOrderedSame;
    }
    
    return (myValue < otherValue ? NSOrderedAscending : NSOrderedDescending);
}

@end
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                          @"11111", @"1",
                          @"33333", @"3",
                          @"22222", @"2",
                          @"44444", @"4",
                          @"55555", @"5",
                          @"88888", @"8",
                          @"77777", @"7",
                          @"66666", @"6",
                          nil];
    
NSArray
*key = [dict keysSortedByValueUsingSelector:@selector(intCompare:)]; NSLog(@"%@", key); NSLog(@"%@", dict);

 

posted @ 2013-05-24 17:53  九勺吹雪  阅读(731)  评论(0)    收藏  举报