来自indexesOfObjectsPassingTest 的排序数组?

发布于 2024-11-17 11:00:25 字数 753 浏览 6 评论 0原文

我有一个名为 arrayToFilter 的 myObjects 数组。 myObject 的一个(元素?)是一组贝塞尔路径。我将特定索引(thispath)处的贝塞尔路径与第二条路径进行比较,并使filteredArray仅由路径重叠一定量(20%)的那些对象组成。我使用indexedOfObjectsPassingTest来喜欢这个:

    NSIndexSet * index = [[arrayToFilter objectsAtIndexes:index]       indexesOfObjectsPassingTest:^BOOL (id obj, NSUInteger idx, BOOL *stop){
    MyObject * anobject = obj;
        UIBezierPath * thispath = [anobject.allPaths objectAtIndex:i];
        NSInteger  overlap = [self percentPathBoxOverlap:path: thispath];

        return overlap>20;
    }];

    if ([index count] !=0){
        filteredArray = [arrayToFilter objectsAtIndexes:index] ;
    }

这工作正常。但我想做的是让filteredArray 与那些重叠值较高的对象一起排序。由于重叠是根据当前路径和此路径动态计算的,因此我不知道如何使用任何排序数组方法。

I have an array of myObjects called arrayToFilter. One (element?) of myObject is an array of bezierpaths. I am comparing the bezierpath at a particular index (thispath) to a second path, and making filteredArray composed of only those objects in which the paths overlapped a certain amount (20%). I used indexedOfObjectsPassingTest to like this:

    NSIndexSet * index = [[arrayToFilter objectsAtIndexes:index]       indexesOfObjectsPassingTest:^BOOL (id obj, NSUInteger idx, BOOL *stop){
    MyObject * anobject = obj;
        UIBezierPath * thispath = [anobject.allPaths objectAtIndex:i];
        NSInteger  overlap = [self percentPathBoxOverlap:path: thispath];

        return overlap>20;
    }];

    if ([index count] !=0){
        filteredArray = [arrayToFilter objectsAtIndexes:index] ;
    }

This works fine. But what i'd like to do is have filteredArray come out sorted with those object with the higher value for overlap coming out first. since overlap is calculated on the fly based on the current path and thispath, i don't know how to use any of the sorted array methods.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪意 2024-11-24 11:00:25

您可以首先创建一个包含路径和重叠数据的字典数组。这将需要对当前通过过滤器搜索和提取的方法进行一些修改。

NSMutableArray * searchResults = [NSMutableArray array];
[arrayToSearch enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
   MyObject     * anobject = obj;
   UIBezierPath * thispath = [anobject.allPaths objectAtIndex:i];

   NSInteger  overlap = [self percentPathBoxOverlap:path: thispath]; 

   if ( overlap > 20 ) {
       NSMutableDictionary * dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:anObject, @"object", [NSNumber numberWithInteger:overlap], @"overlap", nil];
       [searchResults addObject:dictionary];
   }
}];

现在您可以使用字典的 overlap 键对此数组进行排序。

NSSortDescriptor * descriptor = [NSSortDescriptor sortDescriptorWithKey:@"overlap" ascending:NO];
NSArray * sortedArray = [searchResults sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];

现在,sortedArray 将具有路径和重叠信息的排序列表。

You can start off by creating an array of dictionaries containing both path and the overlap data. This will require some modification to your current approach where you search and extract over filter.

NSMutableArray * searchResults = [NSMutableArray array];
[arrayToSearch enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
   MyObject     * anobject = obj;
   UIBezierPath * thispath = [anobject.allPaths objectAtIndex:i];

   NSInteger  overlap = [self percentPathBoxOverlap:path: thispath]; 

   if ( overlap > 20 ) {
       NSMutableDictionary * dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:anObject, @"object", [NSNumber numberWithInteger:overlap], @"overlap", nil];
       [searchResults addObject:dictionary];
   }
}];

Now you can sort this array using the overlap key of the dictionaries.

NSSortDescriptor * descriptor = [NSSortDescriptor sortDescriptorWithKey:@"overlap" ascending:NO];
NSArray * sortedArray = [searchResults sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];

Now sortedArray will have the sorted list of path and overlap information.

兲鉂ぱ嘚淚 2024-11-24 11:00:25

您必须首先对数组进行排序,然后调用 indexesOfObjectsPassingTest 来获取排序后的索引。 sortedArrayUsingComparator:是对数组进行排序的更简单的方法之一,它需要一个块,就像indexesOfObjectsPassingTest 方法。

NSArray arrayToFilter = [originalArray sortedArrayUsingComparator: ^(id a, id b) 
{ 
  if (a.someValue > b.someValue) return NSOrderedAscending; 
  if (a.someValue < b.someValue) return NSOrderedDescending;
  return NSOrderedSame;
}];

然后您可以在 arrayToFilter 上执行现有的过滤

You'll have to sort the array first, then call indexesOfObjectsPassingTest for the sorted indices. sortedArrayUsingComparator: is one of the easier methods of sorting an array, it takes a block just like the indexesOfObjectsPassingTest method.

NSArray arrayToFilter = [originalArray sortedArrayUsingComparator: ^(id a, id b) 
{ 
  if (a.someValue > b.someValue) return NSOrderedAscending; 
  if (a.someValue < b.someValue) return NSOrderedDescending;
  return NSOrderedSame;
}];

Then you can perform your existing filtering on the arrayToFilter

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文