0

我有一个包含字典的数组(seachResult),我想根据字典中的“价格”键对该数组进行排序,该键是字符串格式的数字。我尝试了这段代码,但它不适用于排序“价格”,但它适用于作为数字的 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 \na \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);

但仍然无法正常工作。

4

2 回答 2

2

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

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

数组中的第二个对象,其格式如下price = "125.99";(可能是 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];
    }];
于 2013-07-16T23:32:37.920 回答
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 的价格,它可能会崩溃。

于 2013-07-16T23:53:22.197 回答