iPhone - 按 CGRect 的 origin.x 对字典数组进行排序

发布于 2025-01-04 00:33:17 字数 300 浏览 1 评论 0原文

我有一系列字典。 每个字典都是这样的

object, @"myObject",
[NSValue valueWithCGRect:myRect], @"rect",
nil

如何按 myRect.origin.x 对该数组进行排序?

我知道 Objective-C 有智能的方法来对字典数组进行排序。我的问题是,我想按 myRect 的 x 坐标进行排序,而 myRect 包含在 NSValue 中...有什么方法可以使用魔术命令来做到这一点吗?我知道你们都是魔术师! :D

谢谢。

I have an array of dictionaries.
Each dictionary is like this

object, @"myObject",
[NSValue valueWithCGRect:myRect], @"rect",
nil

How do I sort that array by myRect.origin.x ?

I know that objective-c has smart ways to sort arrays of dictionaries. My problem here is that I want to sort by the x coordinate of myRect and myRect is wrapped in a NSValue... any way to do that with a magic command? I know you guys are magicians! :D

thanks.

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

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

发布评论

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

评论(3

下雨或天晴 2025-01-11 00:33:17

最简洁的方法可能是使用像 sortUsingComparator:sortedArrayUsingComparator 这样的方法(取决于数组的可变性)并定义一个自定义比较器(一个带有两个参数并返回的块)一个 NSComparisonResult) 来解压你的矩形并比较它们的 x 坐标。

编辑:好吧,既然其他人都提供了代码,这是我的:

[array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    CGRect rect1 = [[obj1 valueForKey:@"rect"] CGRectValue];
    CGRect rect2 = [[obj2 valueForKey:@"rect"] CGRectValue];

    if(rect1.origin.x < rect2.origin.x) {
        return NSOrderedAscending;
    } else if(rect1.origin.x > rect2.origin.x) {
        return NSOrderedDescending;
    } else {
        return NSOrderedSame;
    }
}];

The cleanest way is probably to use a method like sortUsingComparator: or sortedArrayUsingComparator (depending on the mutability of the array) and define a custom comparator (a block taking two parameters and returning an NSComparisonResult) that unpacks your rects and compare their x-coordinates.

edit: Well, since everyone else is providing code, here's mine:

[array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    CGRect rect1 = [[obj1 valueForKey:@"rect"] CGRectValue];
    CGRect rect2 = [[obj2 valueForKey:@"rect"] CGRectValue];

    if(rect1.origin.x < rect2.origin.x) {
        return NSOrderedAscending;
    } else if(rect1.origin.x > rect2.origin.x) {
        return NSOrderedDescending;
    } else {
        return NSOrderedSame;
    }
}];
送舟行 2025-01-11 00:33:17

没有什么魔法,但它也不是免费的。 :) 您正在寻找 -[NSArray SortedArrayUsingComparator:]。如果您的数组是 NSMutableArray 那么您可以使用 -[NSMutableArray sortUsingComparator:]

比较器是一个返回 NSComparisonResult 并接受两个对象的块。这是 NSObjCRuntime.h 中的定义:

typedef NSComparisonResult (^NSComparator)(id obj1, id obj2);

该块应该获取对象,将它们转换为字典,将每个对象中的 NSValue 提取到矩形中,并返回比较 X 坐标字段的结果。

伪代码,假设有一个 NSMutableArray

[myArray sortUsingComparator:^(id obj1, id obj2) {
    NSDictionary *dict1 = (NSDictionary *)obj1;
    NSDictionary *dict2 = (NSDictionary *)obj2;
    // Extract each of the NSValues
    // Compare the x values
    if (xValue1 == xValue2) return NSOrderedSame;
    return (xValue1 < xValue2) ? NSOrderedAscending : NSOrderedDescending;
}];

There's no magic, but it's not free, either. :) You're looking for -[NSArray sortedArrayUsingComparator:]. If your array is an NSMutableArray then you can use -[NSMutableArray sortUsingComparator:].

A comparator is a block which returns an NSComparisonResult and takes two objects. Here's the definition in NSObjCRuntime.h:

typedef NSComparisonResult (^NSComparator)(id obj1, id obj2);

The block should take the objects, cast them to dictionaries, extract the NSValues from each back into rects and return the result of comparing the X coordinate fields.

Psuedocode, assuming an NSMutableArray:

[myArray sortUsingComparator:^(id obj1, id obj2) {
    NSDictionary *dict1 = (NSDictionary *)obj1;
    NSDictionary *dict2 = (NSDictionary *)obj2;
    // Extract each of the NSValues
    // Compare the x values
    if (xValue1 == xValue2) return NSOrderedSame;
    return (xValue1 < xValue2) ? NSOrderedAscending : NSOrderedDescending;
}];
り繁华旳梦境 2025-01-11 00:33:17

这应该可以做到(恐怕这不完全是一句台词):

NSArray *sortedArray = [unsortedArray sortedArrayUsingComparator:^(id obj1, id obj2) {
    ([[obj1 objectForKey:@"rect"] CGRectValue].x < [[obj2 objectForKey:@"rect"] CGRectValue].x)? NSOrderedAscending : NSOrderedDescending;
}];

This should do it (it's not quite a one-liner I'm afraid):

NSArray *sortedArray = [unsortedArray sortedArrayUsingComparator:^(id obj1, id obj2) {
    ([[obj1 objectForKey:@"rect"] CGRectValue].x < [[obj2 objectForKey:@"rect"] CGRectValue].x)? NSOrderedAscending : NSOrderedDescending;
}];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文