2013-07-16 75 views
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 \呐 \ 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

你能告诉我们什么是self.searchResult?我用我自己的输入数据尝试了你的代码,它工作得很好。 –

+0

当然,我已经更新了问题 – John

回答

2

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

数组中的第一个对象按照以下格式price = 12;(可能是NSDecimalNumber

采用以下格式price = "125.99";(proably一个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]; 
    }]; 
+0

是的,你是正确的,我改变了代码,它可以很好地通过pid进行排序,这是一个数字,但是当我将其更改为数字排序的价格时,但是以字符串格式排序它不起作用。 我该怎么办? – John

+0

我认为你应该确保检查实际价格是多少。因为就像我写的,它似乎是在你的数组输出中的两个不同的类。为了能够按照您希望排序的方式对数组进行排序,价格对象需要具有相同的类。 –

+0

它来自mysql数据库,它定义为Varchar那里,输出是在json和app从json数据读取的。 我怎样才能让他们一样? – John

0

由于数组中的第二个对象的价格数据似乎要改变(NSString vs 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以外的价格购买其他产品,它可能会崩溃。

+0

谢谢,是的,这是正确的方式,对不起,我没有足够的信贷投票了 – John