如何使用以下方法对数组进行排序,保留重复项?我想要的是对距离数组进行排序,并使lineItems数组按相同的顺序排序,以便我的行项目按距离排序。是否有捷径可寻?我尝试了很多不同的实现,没有运气。
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];
}
我的冒泡排序实施:
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];
}
}
}
答案 0 :(得分:3)
您的问题不是-sortedArrayUsingSelector:
,NSDictionary
-allKeys
方法。键在字典中是唯一的,因此-allKeys
数组不会有任何重复。
使用单独的NSArray
实例存储排序结果,或使用inafziger的建议。
答案 1 :(得分:1)
请尝试以下方法,而不是使用排序功能:
[distanceArray sortArrayUsingSelector:@selector(compare:)];
然后根本不需要字典。
答案 2 :(得分:0)
为什么不做旧的时尚泡泡排序呢?
类似的东西:
distanceArray=[NSMutableArray arrayWithOjects:[ distanceArray allKeys]];
for (int i=0;i<[distanceArray count]-1;i++){
for(int j=1;j<[distanceArray count];j++){
if ([[distanceArray objectAtIndex:i]floatValue] >[[distanceArray objectAtIndex:j]floatValue]){
id temp=[[distanceArray objectAtIndex:i]floatValue];
[distanceArray insertObject:[[distanceArray objectAtIndex:j]floatValue] atIndex:i];
[distanceArray insertObject:temp atIndex:j];
}
}
}
并且它们都是重复排序的(它不漂亮但是有效)
编辑:
如果没有任何作用..制作一个客观的c对象,例如:coord1,coord2,distance,也许是一个唯一的id。所以你永远不会有一个重复的项目。将它们作为该对象的属性声明,然后对它们进行冒泡排序(也许也可以更改id-s)。如果你有一个id = 2的对象并且你在索引1的数组中移动它,那么就改变id以匹配索引...或者那样的东西。然后你不需要字典,因为你在对象本身中有coord ...它应该更容易做..至少在我的脑海里
答案 3 :(得分:0)
我最终只是将lineItems转换为可变字典数组并将距离添加到每个行项目中。然后使用sortUsingDescriptor方法。我感谢大家的帮助!这么简单的事情对我来说太难了...谢谢:
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];
NSMutableDictionary *newLineItem = [[lineItems objectAtIndex:i] mutableCopy];
[newLineItem setObject:foo forKey:@"distance"];
[lineItems removeObjectAtIndex:i];
[lineItems insertObject:newLineItem atIndex:i];
newLineItem = nil;
}
[lineItems sortUsingDescriptors:[NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"distance" ascending:YES]]];