Saadi
Saadi

Reputation: 1294

NSMutableDictionary does not sort correctly

Let i have unsorted NSMutableDictionary

    {
        A = "3";
        B = "2"; 
        C = "4";
    }

And i need result to be like:

    {
        B = "2";
        A = "3";
        C = "4";
    }

How can i achieve this result in objective c.

A simple code implementation will be appreciated.

Upvotes: 1

Views: 1688

Answers (5)

Baig
Baig

Reputation: 4995

You can not sort NSMutableDictionary by value as @joe and @mavrick3 answer. However if you change there keys and values to NSArray you can do it.. Here is simple implementation.. NSMutableDictionary *results; //dictionary to be sorted

    NSMutableDictionary *results; //dict to be sorted
    NSArray *sortedKeys = [results keysSortedByValueUsingComparator: ^(id obj1, id obj2) {
        if ([obj1 integerValue] > [obj2 integerValue]) 
            return (NSComparisonResult)NSOrderedDescending;
        if ([obj1 integerValue] < [obj2 integerValue])
            return (NSComparisonResult)NSOrderedAscending; 
        return (NSComparisonResult)NSOrderedSame;
    }];
    NSArray *sortedValues = [[results allValues] sortedArrayUsingSelector:@selector(compare:)];

    //Descending order
    for (int s = ([sortedValues count]-1); s >= 0; s--) {
        NSLog(@" %@ = %@",[sortedKeys objectAtIndex:s],[sortedValues objectAtIndex:s]); 
    }
    //Ascending order
    for (int s = 0; s < [sortedValues count]; s++) {
        NSLog(@" %@ = %@",[sortedKeys objectAtIndex:s],[sortedValues objectAtIndex:s]); 
    }

Upvotes: 3

Sham
Sham

Reputation: 475

You can try this to sort your Dictionary.

 NSMutableDictionary *tmpDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"6",@"A",@"3",@"B",@"5",@"C",@"2",@"D",@"21",@"F",@"20",@"G",nil];


NSArray *sortedArray = [tmpDict keysSortedByValueUsingComparator:^NSComparisonResult(id obj1,id obj2){


    return [obj1 compare:obj2 options:NSNumericSearch];


}];

NSLog(@"Sorted = %@",sortedArray);

Upvotes: 1

Trausti Thor
Trausti Thor

Reputation: 3774

This is the simplest way to do this

NSArray *arr = [NSArray arrayWithObjects:@"2", @"4", @"1", nil];
NSArray *sorted = [arr sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"Pre sort : %@", arr);
NSLog(@"After sort : %@", sorted);

If you have f.ex. array of dictionary (or model objects), you could do this :

NSDictionary *dict1 = [NSDictionary dictionaryWithObject:@"Mannie" forKey:@"name"];
NSDictionary *dict2 = [NSDictionary dictionaryWithObject:@"Zannie" forKey:@"name"];
NSDictionary *dict3 = [NSDictionary dictionaryWithObject:@"Cannie" forKey:@"name"];
NSArray *peopleIKnow = [NSArray arrayWithObjects:dict1, dict2, dict3, nil];

NSSortDescriptor *sorty = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];

NSArray *results = [peopleIKnow sortedArrayUsingDescriptors:[NSArray arrayWithObject:sorty]];

NSLog(@"Before : %@", peopleIKnow);
NSLog(@"After : %@", results);

Upvotes: 0

Fabio Poloni
Fabio Poloni

Reputation: 8371

NSDictionaryas well as NSMutableDictionary cannot be sorted by value. You can only use a NSArray to sort them. But you have to this with your own code and you won't get the same output as you want.

Upvotes: 0

Joe
Joe

Reputation: 47609

Not possible with an NSMutableDictionary, it is not a sorted structure. You will have to turn it into an NSArray and then sort that. You will then not have a dictionary structure.

Upvotes: 7

Related Questions