Iphone 按对象对NSMutableDictionary键排序?

Iphone 按对象对NSMutableDictionary键排序?,iphone,sorting,nsmutabledictionary,Iphone,Sorting,Nsmutabledictionary,好的。交易如下: 有一个以单词为键的NSMutualDictionary(说出名称)。对象的值是一个NSNumber(如额定值) 我想先用最高评分对它们进行排序 我知道我会这样做: [searchWords keysSortedByValueUsingSelector:@selector(intCompare:)]; 但不确定如何实现intCompare。(比较法) 谁能给我指出正确的方向吗 - (NSComparisonResult) intCompare:(NSString *) othe

好的。交易如下: 有一个以单词为键的NSMutualDictionary(说出名称)。对象的值是一个NSNumber(如额定值)

我想先用最高评分对它们进行排序

我知道我会这样做:

[searchWords keysSortedByValueUsingSelector:@selector(intCompare:)];
但不确定如何实现intCompare。(比较法)

谁能给我指出正确的方向吗

- (NSComparisonResult) intCompare:(NSString *) other
{
//What to do here?
}
我想和{Esben,John,Melvin}找个地方

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

enum {
   NSOrderedAscending = -1,
   NSOrderedSame,
   NSOrderedDescending
};
typedef NSInteger NSComparisonResult;
这是取自苹果公司关于数据类型的开发文档。。。现在你要做的就是检查哪个更大。所有这些都是为你做的。只需传入@selector(compare:)就可以了。因为您的值是NSNumber,NSNumber实现了compare:函数。这就是你想要的:)

这是取自苹果公司关于数据类型的开发文档。。。现在你要做的就是检查哪个更大。所有这些都是为你做的。只需传入@selector(compare:)就可以了。因为您的值是NSNumber,NSNumber实现了compare:函数。这就是你想要的:)

或者您也可以使用,以下是intCompare选择器的实现

- (NSComparisonResult) intCompare:(NSString *) other
{
    int myValue = [self intValue];
    int otherValue = [other intValue];
    if (myValue == otherValue) return NSOrderedSame;
    return (myValue < otherValue ? NSOrderedAscending : NSOrderedDescending);
-(NSComparisonResult)intCompare:(NSString*)其他
{
int myValue=[self intValue];
int otherValue=[other intValue];
如果(myValue==otherValue)返回OrderedName;
返回值(myValue
}

或者您也可以使用,以下是intCompare选择器的实现

- (NSComparisonResult) intCompare:(NSString *) other
{
    int myValue = [self intValue];
    int otherValue = [other intValue];
    if (myValue == otherValue) return NSOrderedSame;
    return (myValue < otherValue ? NSOrderedAscending : NSOrderedDescending);
-(NSComparisonResult)intCompare:(NSString*)其他
{
int myValue=[self intValue];
int otherValue=[other intValue];
如果(myValue==otherValue)返回OrderedName;
返回值(myValue

}

由于放入字典的对象是
NSNumber
实例,因此应该稍微更改方法签名。但全面实施非常容易:

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

事实上,我看不出有什么理由需要执行自己的
intCompare:
方法,如果可以使用
NSNumber
已有的
compare:

既然放入字典的对象是
NSNumber
实例,那么应该稍微更改方法签名。但全面实施非常容易:

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

事实上,我看不出为什么你需要自己使用
intCompare:
方法,而你可以使用
compare:
这个
NSNumber
已经有的方法。

你也应该接受这个答案,这样对其他人可能会有帮助:)我只能接受一个答案(我没有在这里制定规则)PeyloW的回答解决了我的问题。你也应该接受这个答案,这样对其他人可能会有帮助:)我只能接受一个答案(我没有制定规则),PeyloW的回答解决了我的问题。是的,tjats是对的。我的印象是只有NSString进行了比较:implemented。但既然NSNumber也这么做了,就应该这么做。现在我该如何设置A的结束或下降方向?还有一个问题。使用选择器(比较:)按对象对dict排序,但首先使用最小值。我如何改变排序方向?@esbenr-这没有直接的支持。但是您可以使用
-[NSDictionary keysSortedByValueUsingComparator:
进行您自己的反向比较。是的,tjats是正确的。我的印象是只有NSString进行了比较:implemented。但既然NSNumber也这么做了,就应该这么做。现在我该如何设置A的结束或下降方向?还有一个问题。使用选择器(比较:)按对象对dict排序,但首先使用最小值。我如何改变排序方向?@esbenr-这没有直接的支持。但是您可以使用
-[NSDictionary keysSortedByValueUsingComparator:
进行自己的反向比较。