John
John

Reputation: 7

Sort an Array of Dictionaries issue

I have an array(seachResult) which contains dictionaries and I want to sort this array according to 'price' key in the dictionary that is a number in string format. I tried this code but it doesn't work for sorting 'price' but it works for pid which is a number. how can I sort it according 'price' (number in string format)?

   NSSortDescriptor *sortByPrice = [NSSortDescriptor sortDescriptorWithKey:@"price" ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortByPrice];
    NSArray *sortedArray = [self.seachResult sortedArrayUsingDescriptors:sortDescriptors];
    NSLog(@"%@",sortedArray );

here is sample data for self.searchResult:

2013-07-17 02:04:55.012 MyApp[57014:16a03] sorted array of dictionaries: (
    {
    cid = 2;
    image = "http:///images/loginlogo.png";
    latitude = "48.245565";
    longitude = "16.342333";
    manual = "";
    movie = "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v";
    pcode = 023942435228;
    pid = 1;
    pname = "example product";
    price = "12.00";
    qrcode = "";
    rid = 1;
    rname = "Example Retailer Name";
    sale = 0;
    "sale_percent" = 0;
    "sale_price" = "0.00";
    text = "here is text about sample product number 1...\nasdasdasda\nsdfsdfsd\nSdfsdf\nSDfsdfs\ndfsdfsdf\n\n\n";
},
    {
    cid = 2;
    image = "http:///testImage.png";
    latitude = "48.245565";
    longitude = "16.342333";
    manual = "";
    movie = "";
    pcode = 1;
    pid = 2;
    pname = "sample product 2";
    price = "126.00";
    qrcode = "";
    rid = 1;
    rname = "Example Retailer Name";
    sale = 1;
    "sale_percent" = 20;
    "sale_price" = "99.99";
    text = "here is text about sample product number 2...\nblah blah blah\nasdasdasd\nASdasdas\nASdasdasd";
},
    {
    cid = 1;
    image = "";
    latitude = "";
    longitude = "";
    manual = "";
    movie = "";
    pcode = 1;
    pid = 3;
    pname = "test product";
    price = "46.00";
    qrcode = "";
    rid = 2;
    rname = "";
    sale = 0;
    "sale_percent" = 0;
    "sale_price" = "35.00";
    text = "some text here...

\nasdasd \nasd \na \nsd \nas \nd"; } )

I also tried this code :

    NSSortDescriptor *hopProfileDescriptor =
    [[NSSortDescriptor alloc] initWithKey:@"price"
                                ascending:YES];

    NSArray *descriptors = [NSArray arrayWithObjects:hopProfileDescriptor, nil];
    NSArray *sortedArrayOfDictionaries = [self.seachResult
                                          sortedArrayUsingDescriptors:descriptors];

    NSLog(@"sorted array of dictionaries: %@", sortedArrayOfDictionaries);

but still doesn't work.

Upvotes: 0

Views: 125

Answers (2)

Peter Warbo
Peter Warbo

Reputation: 11710

I think the issue is that in your self.searchResult array the price data is differently formatted for the objects in the array.

The first object in the array it's formatted like price = 12; (probably a NSDecimalNumber)

The second object in the array it's formatted like price = "125.99"; (proably a NSString)

NSArray *testSorted = [test sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {

        NSString *price1 = obj1[@"price"];
        NSString *price2 = obj2[@"price"];

        NSNumber *n1 = [NSNumber numberWithFloat:[price1 floatValue]];
        NSNumber *n2 = [NSNumber numberWithFloat:[price2 floatValue]];

        return [n1 compare:n2];
    }];

Upvotes: 2

Nicholas Hart
Nicholas Hart

Reputation: 1724

Since your price data seems to vary (NSString vs NSNumber) you might try instead using sortedArrayUsingComparator:. eg:

NSArray *sortedArray = [unsortedArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    id value1 = [obj1 objectForKey:@"price"];
    float float1 = [value1 floatValue];
    id value2 = [obj2 objectForKey:@"price"];
    float float2 = [value2 floatValue];
    if (float1 < float2) {
        return NSOrderedAscending;
    }
    else if (float1 > float2) {
        return NSOrderedDescending;
    }
    return NSOrderedSame;
}];

This is kinda ugly but should get you started. It's also fragile--if you get something other than NSNumber or NSString for the price it'll likely crash.

Upvotes: 0

Related Questions