Ios iphonesdk中的排序数组

Ios iphonesdk中的排序数组,ios,objective-c,cocoa-touch,sorting,Ios,Objective C,Cocoa Touch,Sorting,在我的应用程序中,我有两个数组名listofevents和arr\u Distance 事件列表如下所示: ( { Latitude = "34.1356031"; Longitude = "-118.0312634"; VenueName = "Fantasia Billiard"; }, { Latitude = "34.1356031"; Longitude = "-118.0312634";

在我的应用程序中,我有两个数组名
listofevents
arr\u Distance

事件列表如下所示:

(
    {
        Latitude = "34.1356031";
        Longitude = "-118.0312634";
        VenueName = "Fantasia Billiard";
    }, {
        Latitude = "34.1356031";
        Longitude = "-118.0312634";
        VenueName = "Dave & Busters";
    }
)
arr\u距离看起来像

(54985.545554985.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]];
}
问题

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


我没有发现我的问题。帮我解决这个问题

问题很简单。在字典中,每个键都应该是唯一的。对你来说不是


您的按键:54985.5455和54985.5455,两者都是相同的。这就是问题发生的原因。更改其中一个,它将像一个符咒一样工作。

您将距离用作关键点,但每个关键点都必须是唯一的。由于距离相同,关键点的后一个对象将覆盖第一个对象

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

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



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

您可以尝试以下代码。我稍微修改了我以前的解决方案,以避免使用字典。早期关于字典的建议纯粹是基于这样的假设,即键是唯一的,而现在看来情况并非如此。所以你可以试试这个:

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

您可以根据结果执行搜索。我从苹果公司发现了以下内容

-(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 */
NSCaseInsensitiveSearch=1,
NSLiteralSearch=2,/*逐字符精确等效*/
NSBackardsSearch=4,/*从源字符串末尾搜索*/
NSAnchoredSearch=8,/*搜索仅限于源字符串的开始(或结束,如果是nsBackardsSearch)*/
NSNumericSearch=64,/*添加到10.2中;字符串中的数字使用数值进行比较,即Foo2.txt“aaa”)*/
NSRegularExpressionSearch/*适用于以下字符串的范围:…、StringByReplacingOccurrencessofString:…、以及ReplaceCurrencessofString:。。。仅方法;搜索字符串被视为ICU兼容的正则表达式;如果设置,则除了NSCaseInsensitiveSearch和NSAnchoredSearch之外,其他选项都不能应用*/

我知道,但还有其他解决方案吗???@user1673099:如果需要使用NSDictionars,请使用唯一键。我在点之间的距离上遇到了错误,如兼容指针类型。计算距离是您的职责。我不知道,你的
点之间的距离是什么样子的。谢谢回复!!好吧,我试试,等等!你只是复制和粘贴吗?这在堆栈溢出上不会经常起作用,因为浏览器不会检查语法并建议代码完成。谢谢@ACB,这对我来说意义重大。谢谢你的回复!!是的,它起作用了!但是你能解释一下之前的问题是什么吗?@user1673099,正如我在回答中提到的,该解决方案基于第一个数组具有所有唯一值的假设。创建具有重复键的词典时,它将只接受最后一个键。因此,您的第一个将被覆盖,并且该值将始终是与第二个对应的值。你的情况就是这样。总是得到第二个值而不是第一个值。
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 */