按对象对 NSMutableDictionary 键进行排序?

发布于 2024-11-28 00:13:16 字数 702 浏览 0 评论 0原文

好吧。这是交易: 有一个 NSMutualDictionary,以单词为键(比如名字)。值对象是一个 NSNumber (如评级),

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:[NSNumber intValue:1] forKey:@"Melvin"];
[dictionary setObject:[NSNumber intValue:2] forKey:@"John"];
[dictionary setObject:[NSNumber intValue:3] forKey:@"Esben"];

我想首先对它们进行最高评级的排序。

我知道我会这样做:

[searchWords keysSortedByValueUsingSelector:@selector(intCompare:)];

但不确定如何实现 intCompare。 (比较方法)

有人能指出我正确的方向吗?

- (NSComparisonResult) intCompare:(NSString *) other
{
//What to do here?
}

我想用 {Esben, John, Melvin} 获得一个 NSArray。

Okeh. Here is the deal:
Have have a NSMutualDictionary with words as keys (say names). The value objects is a NSNumber (like rating)

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:[NSNumber intValue:1] forKey:@"Melvin"];
[dictionary setObject:[NSNumber intValue:2] forKey:@"John"];
[dictionary setObject:[NSNumber intValue:3] forKey:@"Esben"];

I want to sort them with the highest ratings first.

I know I'm going to do it like this:

[searchWords keysSortedByValueUsingSelector:@selector(intCompare:)];

But not sure how to implement intCompare. (the compare method)

Can anybody point me in the right direction?

- (NSComparisonResult) intCompare:(NSString *) other
{
//What to do here?
}

I want to get an NSArray with {Esben, John, Melvin}.

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

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

发布评论

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

评论(3

最近可好 2024-12-05 00:13:16

由于放入字典中的对象是 NSNumber 实例,因此您应该稍微更改方法签名。但完整的实现非常简单:

-(NSComparisonResult)intCompare:(NSNumber*)otherNumber {
    return [self compare:otherNumber];
}

事实上,当您可以使用 compare: 时,我认为没有理由需要执行自己的 intCompare: 方法NSNumber 已经有了。

Since the objects you put into the dictionary are NSNumber instances you should change the method signature a bit. But the full implementation is really easy:

-(NSComparisonResult)intCompare:(NSNumber*)otherNumber {
    return [self compare:otherNumber];
}

In fact I see no reason as to why you need to do your own intCompare: method, when you could go with the compare: that NSNumber already has.

苍白女子 2024-12-05 00:13:16
These constants are used to indicate how items in a request are ordered.

enum {
   NSOrderedAscending = -1,
   NSOrderedSame,
   NSOrderedDescending
};
typedef NSInteger NSComparisonResult;

这是取自苹果关于数据类型的开发文档......现在您所要做的就是检查哪一个更大。不过,这一切都是为你做的。只需传入 @selector(compare:) 即可。由于您的值是 NSNumbers 并且 NSNumber 实现了 Compare: 函数。这就是你想要的:)

These constants are used to indicate how items in a request are ordered.

enum {
   NSOrderedAscending = -1,
   NSOrderedSame,
   NSOrderedDescending
};
typedef NSInteger NSComparisonResult;

That is taken from Apple's dev documentataion on Data types... now all you have to do is check which one is bigger. All of this is done for you though. Simply pass in @selector(compare:) and that should do it. As your values are NSNumbers and NSNumber implements the compare: function. Which is what you want :)

人生百味 2024-12-05 00:13:16
NSArray *sortedArray = [searchWords sortedArrayUsingSelector:@selector(compare:) ];

或者您可能会使用,这是您的 intCompare 选择器的实现

- (NSComparisonResult) intCompare:(NSString *) other
{
    int myValue = [self intValue];
    int otherValue = [other intValue];
    if (myValue == otherValue) return NSOrderedSame;
    return (myValue < otherValue ? NSOrderedAscending : NSOrderedDescending);

}

NSArray *sortedArray = [searchWords sortedArrayUsingSelector:@selector(compare:) ];

or you might well as used, here is the implementation of your intCompare selector

- (NSComparisonResult) intCompare:(NSString *) other
{
    int myValue = [self intValue];
    int otherValue = [other intValue];
    if (myValue == otherValue) return NSOrderedSame;
    return (myValue < otherValue ? NSOrderedAscending : NSOrderedDescending);

}

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