gpt4 book ai didi

objective-c - 首先尝试对数组进行排序

转载 作者:行者123 更新时间:2023-11-29 13:24:51 25 4
gpt4 key购买 nike

我正在尝试按照我分配给数组中每个条目的最大数字对数组进行排序。

但是我不认为它在做任何事情。关于错误可能出在哪里的任何建议?

谢谢!

 - (NSArray*)sortByPercentage:(NSArray*)array {

NSArray *inputArray = array;
NSSortDescriptor *sortDescriptor = [[ NSSortDescriptor alloc] initWithKey:@"percentMatched" ascending:YES];

//nov 8
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *finalResult = [inputArray sortedArrayUsingDescriptors:sortDescriptors];
[sortDescriptor release];
return [NSArray arrayWithArray:finalResult];
}

最佳答案

我很好奇为什么您的排序不起作用。这样做:

#import <Foundation/Foundation.h>

@interface FooObject : NSObject
@property (nonatomic, assign) NSInteger value;
@property (readonly) NSInteger percentMatched;
@end

@implementation FooObject
@synthesize value;

// compute percentMatched as an elementary function of value
- (NSInteger)percentMatched {
return value * 2;
}
@end

int main(int argc, char *argv[]) {
NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];

FooObject *foo1 = [[FooObject alloc] init];
foo1.value = 50;

FooObject *foo2 = [[FooObject alloc] init];
foo2.value = 5;

FooObject *foo3 = [[FooObject alloc] init];
foo3.value = 10;

NSArray *myFoos = [NSArray arrayWithObjects:foo1,foo2,foo3,nil];
NSArray *sorters = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"percentMatched" ascending:YES]];
NSArray *mySortedFoos = [myFoos sortedArrayUsingDescriptors:sorters];
for(FooObject *foo in mySortedFoos ) {
printf("%ld ",foo.percentMatched);
}
}

按预期将 10 20 100 打印到控制台。

关于objective-c - 首先尝试对数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13426491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com