【问题标题】:applying two sorting doesn't work properly应用两次排序无法正常工作
【发布时间】:2013-08-14 16:19:47
【问题描述】:

我正在尝试先按价格然后按标题对大量产品进行排序,然后再显示它们:

应用这两种排序似乎不起作用,但是,应用其中一种效果很好,所以如果我应用按价格排序,它将返回按价格排序的产品,标题也是如此。那么为什么我不能同时按价格和标题排序呢?

//Sort by Price, then by Title. Both Ascending orders
arrayProduct = (NSMutableArray*)[arrayProduct sortedArrayUsingFunction:priceComparator context:nil];
arrayProduct = (NSMutableArray*)[arrayProduct sortedArrayUsingFunction:titleComparator context:nil];


//products comparators
NSInteger priceComparator(NSMutableDictionary *obj1, NSMutableDictionary *obj2, void *context){


    int v1 = [[[obj1 valueForKey:@"Product Sale Price"]substringFromIndex:1] intValue];
    int v2 = [[[obj2 valueForKey:@"Product Sale Price"]substringFromIndex:1] intValue];

    if (v1 < v2){

        return NSOrderedAscending;
    }
    else if (v1 > v2){

        return NSOrderedDescending;

    }
    else
        return NSOrderedSame;

}

NSInteger titleComparator(NSMutableDictionary *obj1, NSMutableDictionary *obj2, void *context){

    NSString* v1 = [obj1 valueForKey:@"Product Title"];
    NSString* v2 = [obj2 valueForKey:@"Product Title"];

    if ([v1 caseInsensitiveCompare:v2] == NSOrderedAscending){

        return NSOrderedAscending;
    }
    else if ([v1 caseInsensitiveCompare:v2] == NSOrderedDescending){

        return NSOrderedDescending;
    }
    else
        return NSOrderedSame;

}

【问题讨论】:

  • 我不相信sortedArrayUsingFunction:context: 保证保留元素的任何“次要”顺序。来自文档:“返回一个新数组,该数组按比较函数比较器定义的升序列出接收数组的元素。”
  • 那么,按两个参数(价格然后标题)对数组进行排序的更好方法是什么?

标签: ios objective-c sorting


【解决方案1】:

您得到的结果不正确,因为您对整个数组进行了两次排序。以下是一些其他选项:

你可以使用积木

[arrayProduct sortUsingComparator:^NSComparisonResult(id a, id b) {
    NSMutableDictionary * dictA = (NSMutableDictionary*)a;
    NSMutableDictionary * dictB = (NSMutableDictionary*)b;

    NSInteger p1 = [[[dictA valueForKey:@"Product Sale Price"]substringFromIndex:1] integerValue];
    NSInteger p2 = [[[dictB valueForKey:@"Product Sale Price"]substringFromIndex:1] integerValue];

    if(p1 > p2){
        return NSOrderedAscending;
    }
    else if(p2 > p1){
        return NSOrderedDescending;
    }
    //Break ties with product titles
    NSString* v1 = [dictA valueForKey:@"Product Title"];
    NSString* v2 = [dictB valueForKey:@"Product Title"];

    return [v1 caseInsensitiveCompare:v2];
}];

或 NSSortDescriptors (http://developer.apple.com/library/ios/documentation/cocoa/Conceptual/SortDescriptors/Articles/Creating.html)

【讨论】:

  • 您好 iKaver,您测试过上面的示例吗?它没有正确排序
  • 嗨 Malloc,我忘了在示例中提到 arrayProduct 数组没有被排序,它返回一个排序数组。我将编辑代码以澄清。
  • 我对其进行了修改,以便对数组本身进行排序。让我知道这是否能解决您的问题:)
【解决方案2】:

我认为这里的问题是您按价格排序一次,然后按标题对整个数组进行排序,覆盖第一轮完成的任何排序。看起来你想要的是一个按价格排序的数组,如果两个或多个项目的价格相同,则该子集将按标题排序。为此,您应该只排序一次,因此只有一个 Comparator 函数。您基本上需要将您拥有的两者结合起来。首先使用价格逻辑,但如果 v1 == v2 则使用标题逻辑。

【讨论】:

  • 嗨,我已经得到了这个,并尝试完全按照您所说的方式将两种排序结合起来。但它没有返回我想要的结果
猜你喜欢
  • 2020-12-14
  • 2012-03-29
  • 2018-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多