Wie kann ich die folgende Methode verwenden, um mein Array zu sortieren und die die Duplikate zu halten? Alles, was ich will, ist das Sortieren des Distanz-Arrays und das Sortieren des lineItems-Arrays in der gleichen Reihenfolge, so dass meine Werbebuchungen nach Entfernung sortiert sind. Gibt es einen einfachen Weg, dies zu tun? Ich habe viele verschiedene Implementierungen ohne Glück ausprobiert.Wie verhindern Sie, dass sortedArrayUsingSelector doppelte Einträge (ios) entfernt?
lineItems = [[NSMutableArray alloc] initWithArray:(NSMutableArray *)[data objectForKey:@"line_items"]];
distanceArray = [[NSMutableArray alloc] initWithCapacity:[lineItems count]];
for (int i = 0; i < [lineItems count]; i++) {
CLLocation *spotLocation = [[CLLocation alloc] initWithLatitude:[[[lineItems objectAtIndex:i] objectForKey:@"latitude"] floatValue] longitude:[[[lineItems objectAtIndex:i] objectForKey:@"longitude"] floatValue]];
CLLocationDistance distance = ([myLocation distanceFromLocation:spotLocation]/1000) * 0.621371192;
NSNumber *foo = [[NSNumber alloc] initWithDouble:distance];
[distanceArray insertObject:foo atIndex:i];
}
Meine Blase Art Umsetzung:
for (int i=0;i<[distanceArray count]-1;i++){
for(int j=1;j<[distanceArray count];j++){
if ([[distanceArray objectAtIndex:i]doubleValue] >[[distanceArray objectAtIndex:j]doubleValue]){
NSNumber *temp_i = [[NSNumber alloc] initWithDouble:[[distanceArray objectAtIndex:i]doubleValue]];
NSNumber *temp_j = [[NSNumber alloc] initWithDouble:[[distanceArray objectAtIndex:j]doubleValue]];
[distanceArray removeObjectAtIndex:i];
[distanceArray removeObjectAtIndex:j];
[distanceArray insertObject:temp_j atIndex:i];
[distanceArray insertObject:temp_i atIndex:j];
NSDictionary *tempObj_i = [[NSDictionary alloc] initWithDictionary:[lineItems objectAtIndex:i]];
NSDictionary *tempObj_j = [[NSDictionary alloc] initWithDictionary:[lineItems objectAtIndex:j]];
[lineItems removeObjectAtIndex:i];
[lineItems removeObjectAtIndex:j];
[lineItems insertObject:tempObj_j atIndex:i];
[lineItems insertObject:tempObj_i atIndex:j];
}
}
}
Was ist in 'dictionary'? – lnafziger
Inafziger, ich habe mehr von dem Code hinzugefügt –
auf welche Methode beziehen Sie sich? 'sortedArrayUsingSelector:'? – skytz