向阵列中添加两个NSMutableArray并在ios中使用升序筛选数据
我正在做定位部分,通过像这样将距离添加到NSMutableArray中来获取for循环中的距离 此号码存储为NSNumber 3.2, 1.2, 5. , , 像这样,在一个例子中,我从一个数组中的核心数据中获取数据,像这样作为一个名称。。。 此名称存储为NSString D B A. C , , 像这样在这里 3.2属于D类, 1.2属于B, , , 像这样,我想用距离过滤这些数据 喜欢 1.2, 3.2, 5. , , 像这样但我做不到需要一些帮助 这是我从核心数据中获取数据的代码向阵列中添加两个NSMutableArray并在ios中使用升序筛选数据,ios,objective-c,iphone,arrays,Ios,Objective C,Iphone,Arrays,我正在做定位部分,通过像这样将距离添加到NSMutableArray中来获取for循环中的距离 此号码存储为NSNumber 3.2, 1.2, 5. , , 像这样,在一个例子中,我从一个数组中的核心数据中获取数据,像这样作为一个名称。。。 此名称存储为NSString D B A. C , , 像这样在这里 3.2属于D类, 1.2属于B, , , 像这样,我想用距离过滤这些数据 喜欢 1.2, 3.2, 5. , , 像这样但我做不到需要一些帮助 这是我从核心数据中获取数据的代码 NSFe
NSFetchRequest * dealFetch = [NSFetchRequest fetchRequestWithEntityName:@"Deal"];
NSArray * array = [_managedObjectContext executeFetchRequest:dealFetch error:&error];
NSArray * nameAa = [array valueForKey:@"nameAttribute"];
NSLog(@"%@",nameAa);
这是我得到的代码
g位置
locaFilterArray = [[NSMutableArray alloc]init];
for (int l = 0 ; l < dealArray.count; l++) {
Deal * deal = (Deal *)dealArray[l];
NSArray * locArray = [[deal locationRelation]allObjects];
double minValue = 10000;
if (locArray.count > 0) {
for (int l = 0; l < locArray.count ; l++) {
DealLocation * location = (DealLocation *)locArray[l];
double latitude = [[location latitude]doubleValue];
double longitude = [[location longitude]doubleValue];
CLLocation * cdLocation = [[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
CLLocation * currLocation = [[CLLocation alloc]initWithLatitude:appDelegate.currentLatitude longitude:appDelegate.currentLongitude];
CLLocationDistance distance = [currLocation distanceFromLocation:cdLocation]/1000;
if (distance < minValue) {
minValue = distance;
}
}
} else {
minValue = 5;
}
float va = [[NSString stringWithFormat:@"%.1f",minValue]floatValue];
NSNumber *numb = [NSNumber numberWithFloat:va];
[locaFilterArray addObject:numb];
}
NSLog(@"%@",locaFitlerArray);
locaFilterArray=[[NSMutableArray alloc]init];
for(int l=0;l0){
对于(int l=0;l
哇,我找到了解决方案。请看下面的代码,它是如何工作的
NSFetchRequest * dealFetch = [NSFetchRequest fetchRequestWithEntityName:@"Deal"];
NSArray * array = [_managedObjectContext executeFetchRequest:dealFetch error:&error];
NSArray * nameAa = [array valueForKey:@"nameAttribute"];
NSArray * locArray = [NSArray arrayWithObjects:locaFilterArray, nil];
NSDictionary * dd = [NSDictionary dictionaryWithObjects:locaFilterArray forKeys:nameAa];
NSLog(@"%@",[self sortKeysByIntValue:dd]);
- (NSArray *)sortKeysByIntValue:(NSDictionary *)dictionary {
NSArray *sortedKeys = [dictionary keysSortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2) {
int v1 = [obj1 intValue];
int v2 = [obj2 intValue];
if (v1 < v2)
return NSOrderedAscending;
else if (v1 > v2)
return NSOrderedDescending;
else
return NSOrderedSame;
}];
return sortedKeys;
}
NSFetchRequest*dealFetch=[NSFetchRequest fetchRequestWithEntityName:@“Deal”];
NSArray*数组=[\u managedObjectContext executeFetchRequest:dealFetch错误:&错误];
NSArray*nameAa=[array valueForKey:@“nameAttribute”];
NSArray*locArray=[NSArray ARRAY WITHOBJECTS:locaFilterArray,nil];
NSDictionary*dd=[NSDictionary Dictionary WithObjects:locaFilterArray forKeys:nameAa];
NSLog(@“%@,[self-sortKeysByIntValue:dd]);
-(NSArray*)sortKeysByIntValue:(NSDictionary*)字典{
NSArray*sortedKeys=[使用比较器的字典键SortedByValue:^n比较结果(id obj1,id obj2){
int v1=[obj1 intValue];
int v2=[obj2 intValue];
如果(v1v2)
退而求其次;
其他的
返回指定名称;
}];
返回分拣机;
}
根据距离和值(D,B,…)的相关性对数组进行筛选或排序?我将3.2,1.2,5,…,存储为NSnumbers发布您尝试过的代码..创建一个以名称为键,以数字为值的字典,然后您可以轻松对其进行排序。这很好:float va=[[NSString stringWithFormat:@“%.1f”,minValue]floatValue]代码>。