cocoa - 在描述符之后对数组进行排序

标签 cocoa nsarray sorting

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

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

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

最佳答案

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

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

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

[news sortUsingDescriptors:...]

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

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

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

关于cocoa - 在描述符之后对数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14877220/

相关文章:

cocoa - 如何在 mac os x 中获得 float 窗口

Objective-C - 正确的初始化方法

objective-c - NSArray 来自 NSDictionary 中的数组

jquery 根据其 css ("color"对 dom 元素进行排序)

objective-c - 如何从 NSMatrix 获取 NSButton (CheckBox) 的状态

ios - double iOS 的 NSArray( objective-c )

objective-c - NSArray 使用的排序算法是否稳定?

sorting - grep 并使用模式文件进行排序

javascript - 对线程对话进行排序

swift - 拖入时 NSOutlineView 不显示蓝色指南/分隔符