theandrew
theandrew

Reputation: 33

sort array after descriptor

This is some of my code. And everything works fine. But not the sorting. I want to sort my Array after "Altitude" But it doesn't work so here is my code and i hope that somebody can help me.

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

Can somebody help me how i can fix my problem?

Upvotes: 0

Views: 93

Answers (1)

Martin R
Martin R

Reputation: 539965

sortedArrayUsingDescriptors returns a new sorted array, it does not sort the array itself. You can either use

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

to get a new sorted array, or use sortUsingDescriptors:

[news sortUsingDescriptors:...]

to sort the (mutable) array news itself.

Another problem is that in your for-loop you set the same "Altitude" value for all objects in the array, therefore sorting by that key does not change anything.

Note that newsMutable = [news mutableCopy] creates a new array, but does not copy the elements in the array, so that the following for-loop modifies the items from the news array.

Upvotes: 2

Related Questions