1

Any pointers for the right direction would be gratefully received,

I have a NSDictionary of the format:

{
3 = "special-collection-procedures-see-notes|";
13 = "<p>None Given</p>";
16 = "";
6 = "<p>Arrange for sample to be separated at an interim site and subsequently transport frozen to lab, if it cannot reach lab within one hour.</p>";
9 = "<p>Stored in laboratory at -20C</p>";
12 = "<p>None</p>";
2 = "images/tubes/lightblue_tube_large.png";
15 = "";
5 = "";
8 = "Up to 28 days";
11 = "<p>Some indications: Hereditary Angioedema</p><p>Sample type is citrated plasma though plain serum can also be used.</p>";
1 = " Functional C1 esterase inhibitor";
14 = "|";
4 = "5-10 mL";
7 = "<p>Frozen if unable to deliver within one hour</p>";
10 = "<p>expressed as a percentage of a control serum with 75% -125% being classed as normal.</p>";

}

Which I would like to sort the keys so that the order is:

1 = " Functional C1 esterase inhibitor";
2 = "images/tubes/lightblue_tube_large.png";

Etc.

I have this piece of code:

NSArray *myArray; 

myArray = [[valuesDictionary allKeys] sortedArrayUsingComparator: ^(id obj1, id obj2) {

    if ([obj1 integerValue] > [obj2 integerValue]) {

        return (NSComparisonResult)NSOrderedDescending;
    }
    if ([obj1 integerValue] < [obj2 integerValue]) {

        return (NSComparisonResult)NSOrderedAscending;
    }

    return (NSComparisonResult)NSOrderedSame;
}];

NSMutableDictionary *sortedDisplay = [[NSMutableDictionary alloc] init];

for (id object in myArray) {
    // do something with object
    [sortedDisplay setObject:[valuesDictionary objectForKey:object] forKey:object];

}

Which gives me a sorted myArray: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16

But the final "sorted" mutable dictionary is not sorted i.e its

Printing description of sortedDisplay:

{
13 = "<p>None Given</p>";
12 = "<p>None</p>";
11 = "<p>Some indications: Hereditary Angioedema</p><p>Sample type is citrated plasma though plain serum can also be used.</p>";
7 = "<p>Frozen if unable to deliver within one hour</p>";
6 = "<p>Arrange for sample to be separated at an interim site and subsequently transport frozen to lab, if it cannot reach lab within one hour.</p>";
2 = "images/tubes/lightblue_tube_large.png";
1 = " Functional C1 esterase inhibitor";
14 = "|";
15 = "";
10 = "<p>expressed as a percentage of a control serum with 75% -125% being classed as normal.</p>";
9 = "<p>Stored in laboratory at -20C</p>";
8 = "Up to 28 days";
5 = "";
4 = "5-10 mL";
3 = "special-collection-procedures-see-notes|";
16 = "";

}

What am I doing wrong?

Craig Webster
  • 155
  • 1
  • 11

3 Answers3

3

What am I doing wrong?

Trying to sort a dictionary.

tommyo
  • 531
  • 4
  • 10
3

You're going to want to make a sorted array:

    NSArray *sortedKeys = [dictionary keysSortedByValueWithOptions:NSNumericSearch usingComparator:^NSComparisonResult(NSString *a, NSString *b){
        return [a compare:b];
    }];
    //Now you have an array of sorted keys


    NSMutableArray *sortedArray = [[NSMutableArray alloc] initWithCapacity:sortedKeys.count];
    for (NSString *key in sortedKeys){
        NSDictionary *dict = [NSDictionary dictionaryWithObject:[dictionary valueForKey:key] forKey:key];
        [sortedArray addObject:dict];
    }
    //Now you have a sortedArray of key-value pairs
Jesse Gumpo
  • 4,777
  • 1
  • 20
  • 29
  • Thanks, used this in the end NSEnumerator *sectEnum = [myArray objectEnumerator]; NSMutableArray *index = [[NSMutableArray alloc] init]; id sKey; while((sKey = [sectEnum nextObject])) { if ([valuesDictionary objectForKey:sKey] != nil ) { [index addObject:[valuesDictionary objectForKey:sKey] ]; } } – Craig Webster Jul 14 '12 at 14:51
2

Tommyo is right, you can't sort a NSDictionary. Its purpose is to store and retrieve data efficiently in terms of performance.

If you need to store data in a particular order, use an NSArray for the keys (as you are already doing), and use it to access the dictionary values when you need them.

Xavi Gil
  • 11,460
  • 4
  • 56
  • 71
  • Thanks, was trying to provide a way of displaying the data in a sortable format for a table view. Probably on the wrong track! – Craig Webster Jul 13 '12 at 23:39