排序字典数组问题

时间:2013-07-16 23:07:00

标签: objective-c sorting nsarray nsdictionary

我有一个包含字典的数组(seachResult),我想根据字典中的'price'键对字符串格式的数字进行排序。 我尝试了这个代码,但它不能用于排序'价格',但它适用于pid,这是一个数字。 如何根据“价格”(字符串格式的数字)对其进行排序?

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

这是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 \呐 \ NSD \ NAS \ ND“;     } )

我也试过这段代码:

    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);

但仍然无效。

2 个答案:

答案 0 :(得分:2)

我认为问题在于,在self.searchResult数组中,price数据的格式与数组中的对象格式不同。

数组中的第一个对象,其格式为price = 12;(可能是NSDecimalNumber

数组中的第二个对象,其格式为price = "125.99";(可能是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];
    }];

答案 1 :(得分:0)

由于您的价格数据似乎有所不同(NSString与NSNumber),您可以尝试使用sortedArrayUsingComparator:。例如:

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;
}];

这有点难看,但应该让你开始。它也很脆弱 - 如果你得到NSNumber或NSString以外的其他东西,它可能会崩溃。