0

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

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

有人可以帮我解决我的问题吗?

4

1 回答 1

2

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

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

获得一个新的排序数组,或使用sortUsingDescriptors

[news sortUsingDescriptors:...]

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

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

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

于 2013-02-14T14:53:54.227 回答