-1

在我的应用程序中,我有两个数组名称listofevents& arr_Distance

事件列表如下所示:

(
    {
        Latitude = "34.1356031";
        Longitude = "-118.0312634";
        VenueName = "Fantasia Billiard";
    }, {
        Latitude = "34.1356031";
        Longitude = "-118.0312634";
        VenueName = "Dave & Busters";
    }
)

arr_distance 看起来像

( 54985.5455,54985.5455 )

我已经根据 JSON 响应中给出的纬度和经度计算了距离。在这里,我想按距离递增的顺序显示列表,所以我按照以下代码对两个数组进行了排序

用于排序

NSDictionary *dict = [NSDictionary dictionaryWithObjects:listOfEvents forKeys:arr_distance];
sortedArray = [arr_distance sortedArrayUsingComparator:^(id firstObject, id secondObject) {
    return [((NSString *)firstObject) compare:((NSString *)secondObject) options:NSNumericSearch];
}];
sortedValues = [NSMutableArray array];

for (NSString *key in sortedArray) {
    [sortedValues addObject:[dict valueForKey:key]];
}

问题

但我的问题是,当两个距离相同且场地名称不同时,它只会显示第一个场地名称两次。

我没有找到我的问题。帮我解决这个问题

4

4 回答 4

1

您将距离用作键,但每个键都必须是唯一的。由于您具有相同的距离,因此后面的对象到键将覆盖第一个对象。

我认为最好的解决方案是创建一个自定义类,它包含每个条目,您还可以添加一个属性distance并按其排序。

@interface Entry : NSObject
@property (strong) NSString *longitude
@property (strong) NSString *latitude
@property (strong) NSString *name
@property double distance;
@end 

NSMutableArray *listOfEntries = [NSMutableArray array];
[listofevents enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL *stop) {
    Entry *entry = [[Entry alloc] init];
    entry.latitude = [dict objectForKey:@"Latitude"];
    entry.longitude = [dict objectForKey:@"Longitude"];
    entry.name = [dict objectForKey:@"VenueName"];
    entry.distance = DistanceBetweenPoints(currentLocation, CGPointMake(entry.latitude,entry.longitude));
    [listOfEntries addObject:entry];
}];


[listOfEntries sortUsingComparator:
      ^NSComparisonResult(Entry *obj1, Entry *obj2){
            return obj1.distance > obj2.distance;
    }
];

另一种选择是将名称作为字典中的键。

于 2013-03-04T05:15:40.480 回答
1

你可以试试下面的代码。我稍微修改了我之前的解决方案以避免使用字典。较早的字典建议纯粹基于密钥将是唯一的假设,而现在看来并非如此。所以你可以试试这个:

NSArray *a = @[@"353.90", @"354.68", @"354.68", @"1.18"];
NSArray *b = @[@{ @"Contestant1":@"Jon jones1",
    @"Contestant2":@"Anderson silva1"},
      @{ @"Contestant1":@"Jon jones2",
         @"Contestant2":@"Anderson silva2"},
      @{ @"Contestant1":@"Jon jones3",
         @"Contestant2":@"Anderson silva3"},
      @{ @"Contestant1":@"Jon jones4",
         @"Contestant2":@"Anderson silva4"}];

NSArray *sortedArray = [a sortedArrayUsingComparator:^(id firstObject, id secondObject) {
    return [((NSString *)firstObject) compare:((NSString *)secondObject) options:NSNumericSearch];
}];

NSArray *sortedValues = [b sortedArrayUsingComparator:^(id firstObject, id secondObject) {

    id obj1 = [a objectAtIndex:[b indexOfObject:firstObject]];
    id obj2 = [a objectAtIndex:[b indexOfObject:secondObject]];

    return [((NSString *)obj1) compare:((NSString *)obj2) options:NSNumericSearch];
}];

NSLog(@"%@",sortedArray);
NSLog(@"%@",sortedValues);
于 2013-03-04T08:37:43.570 回答
0

问题很简单。在字典中,每个键都应该是唯一的。在你的情况下,它不是。

你的钥匙:54985.5455 和 54985.5455,都是一样的。这就是问题发生的原因。改变其中一个,它会像一个魅力。

于 2013-03-04T05:14:52.910 回答
0

您可以根据您的结果进行搜索。我从苹果中发现了以下内容

-(NSArray*)SortingArray:(NSMutableArray*)unSortedArray
{
    static NSStringCompareOptions compareOption=NSCaseInsensitiveSearch |NSNumericSearch|NSWidthInsensitiveSearch|NSForcedOrderingSearch;
    NSLocale *locale=[NSLocale currentLocale];
    NSComparator comparatorBlock=^(id  string1, id  string2)
    {
        NSRange string1Range=NSMakeRange(0,[string1 length]);
        return        [string1 compare:string2 options:compareOption range:string1Range locale:locale];
    };
    NSArray *sortedArray=[unSortedArray sortedArrayUsingComparator:comparatorBlock ];
    return sortedArray;
}

您可以使用字符串比较选项。我发现有更多限制搜索的选项

NSCaseInsensitiveSearch = 1,
NSLiteralSearch = 2,        /* Exact character-by-character equivalence */
NSBackwardsSearch = 4,      /* Search from end of source string */
NSAnchoredSearch = 8,       /* Search is limited to start (or end, if NSBackwardsSearch) of source string */
NSNumericSearch = 64,       /* Added in 10.2; Numbers within strings are compared using numeric value, that is, Foo2.txt < Foo7.txt < Foo25.txt; only applies to compare methods, not find */
NSDiacriticInsensitiveSearch  /* If specified, ignores diacritics (o-umlaut == o) */
NSWidthInsensitiveSearch  /* If specified, ignores width differences ('a' == UFF41) */
NSForcedOrderingSearch  /* If specified, comparisons are forced to return either NSOrderedAscending or NSOrderedDescending if the strings are equivalent but not strictly equal, for stability when sorting (e.g. "aaa" > "AAA" with NSCaseInsensitiveSearch specified) */
NSRegularExpressionSearch    /* Applies to rangeOfString:..., stringByReplacingOccurrencesOfString:..., and replaceOccurrencesOfString:... methods only; the search string is treated as an ICU-compatible regular expression; if set, no other options can apply except NSCaseInsensitiveSearch and NSAnchoredSearch */
于 2014-05-06T07:27:11.467 回答