【问题标题】:sort array after descriptor在描述符后排序数组
【发布时间】:2013-02-14 14:38:35
【问题描述】:

这是我的一些代码。 一切正常。但不是排序。我想在“高度”之后对我的数组进行排序 但它不起作用所以这是我的代码,我希望有人可以帮助我。

        //Berechnung der Entfernung
        erdradius = 6371;

        //Koordinaten von Datenbank in String schreiben
        NSString *latitude = [[news objectAtIndex:indexPath.row] objectForKey:@"Latitude"];
        NSString *longitude = [[news objectAtIndex:indexPath.row] objectForKey:@"Longitude"];

        entfernung = 0;
        double lat1 = eigLat;                   //eigener Standort Latitude
        double long1 = eigLon;                  //eigener Standort Longitude
        double lat2 = [latitude doubleValue];   //Standort Heurigen Latitude
        double long2 = [longitude doubleValue]; //Standort Heurigen Longitude

        //Da man mit Bogenmaß rechnen muss!
        double lat1rechnen = lat1*2*M_PI/360;
        double long1rechnen = long1*2*M_PI/360;
        double lat2rechnen = lat2*2*M_PI/360;
        double long2rechnen = long2*2*M_PI/360;

        //Formel zur Berechnung
        entfernung = (erdradius * (2 * asin(sqrt(((sin(((lat1rechnen-lat2rechnen)/2)*(lat1rechnen-lat2rechnen)/2))+cos(lat1rechnen)*cos(lat2rechnen)*(sin(((long1rechnen-long2rechnen)/2)*(long1rechnen-long2rechnen)/2)))))));

        //Werte in Strings damit man sie ausgeben kann
        weg = [NSString stringWithFormat:@"%.2f km", entfernung];
        HeurigenName = [[news objectAtIndex:indexPath.row] objectForKey:@"Name"];


        newsMutable = [news mutableCopy];

        for (NSMutableDictionary* entry in newsMutable)
        {
            [entry setValue:[NSString stringWithFormat:weg] //[NSNumber numberWithDouble:entfernung]
                     forKey:@"Altitude"];
        }


    NSLog(@"%@",newsMutable);

        news = [newsMutable copy];

    NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"Altitude" ascending:YES];
    [news sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];
    wegsorted = [news copy];


    //Ausgabe
    cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"Name"];
    cell.textLabel.textColor = [UIColor whiteColor];
    //cell.detailTextLabel.text =[[news objectAtIndex:indexPath.row] objectForKey:@"Altitude"];
    cell.detailTextLabel.text =[[news objectAtIndex:indexPath.row] objectForKey:@"Altitude"];

    return cell;

谁能帮我解决我的问题?

【问题讨论】:

    标签: cocoa nsarray sorting


    【解决方案1】:

    sortedArrayUsingDescriptors 返回一个新的排序数组,它不对数组本身进行排序。 您可以使用

    NSArray *sortedArray = [news sortedArrayUsingDescriptors:...];
    

    获取一个新的排序数组,或者使用sortUsingDescriptors:

    [news sortUsingDescriptors:...]
    

    对(可变)数组news本身进行排序。

    另一个问题是,在您的 for 循环中,您为数组中的所有对象设置了相同的“高度”值,因此按该键排序不会改变任何内容。

    注意newsMutable = [news mutableCopy]会创建一个新数组,但不会复制数组中的元素,因此下面的for循环会修改news数组中的项目。

    【讨论】:

    • 但是当我在我的 TableView 中给出它时,数组中的每个对象都有另一个 Altutude。但是想在这个高度之后对其进行排序。
    • @theandrew:我很难理解你的代码(即使我懂德语 :-)。似乎在cellForRowAtIndexPath 中调用了这段代码,对吗? - 无法对cellForRowAtIndexPath 中的表格视图进行排序。在显示表格视图之前,您应该只对数组进行一次排序,例如在viewDidLoadviewWillAppear
    • @theandrew:你为什么称它为“高度”?这似乎是一个距离。您应该将 NSNumber 值放入对象中,而不是字符串中,以进行排序。
    • 因为它更容易。我的数组中有 Altitude 并且没有,所以我将它保存在 atitude 中。你认为这是一个坏方法吗?
    • @theandrew:还要注意 CoreLocation 框架有一个方法 distanceFromLocation 可以使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 2014-02-12
    • 2011-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多