开发者

Problem with sorting NSDictionary

开发者 https://www.devze.com 2023-02-03 17:02 出处:网络
I need to sort a NSDictionary of dictionaries. It looks like: {//dictionary RU = \"110.1\"; //key and value

I need to sort a NSDictionary of dictionaries. It looks like:

{//dictionary
        RU = "110.1"; //key and value
        SG =开发者_开发百科 "150.2"; //key and value
        US = "50.3"; //key and value
    }

Result need to be like:

 {//dictionary
            SG = "150.2"; //key and value
            RU = "110.1"; //key and value
            US = "50.3"; //key and value
        }

I am trying this:

@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

This is the result of NSLog: 1)

{
SG,
RU,
US
}

2)

{
150.2,
110.1,
50.3
}

3)

{
            RU = "110.1";
            SG = "150.2";
            US = "50.3";
        }

Why is this happening? Can you help me with this problem?


NSDictionary are unsorted by nature. The order of the objects as retrieved by allKeys and allValues will always be undetermined. Even if you reverse engineer the order it may still change in the next system update.

There is however more powerful alternatives to allKeys that are used to retrieve the keys in a defined and predictable order:

  • keysSortedByValueUsingSelector: - Useful for sorting in ascending order according to the compare: method of the value objects.
  • keysSortedByValueUsingComparator: - New in iOS 4, use a block to do the sort inline.


WOW. Thanx, PeyloW! It's what i needed! I also find this code and it helps me to reorder results:

@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


a NSDictionary is not ordened, so it doens't matter in what order you construct a NSDIctionary.

a NSArray is ordened. If you want to have the NSDictionary ordened in memory, you should somehow make a NSArray of key value pairs. You can also return two NSArrays with corresponding indeces.

If you only want to iterate over the elements way, you can iterate over a sorted array of keys (this is what koregan suggests).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号