gpt4 book ai didi

objective-c - 根据 double 值对包含对象的 NSmutable 数组进行排序

转载 作者:行者123 更新时间:2023-12-03 17:41:56 25 4
gpt4 key购买 nike

我想根据两个对象属性来排序 NSMutableArray 包含对象,其中一个具有 double 类型,第二个具有 NSString

我尝试使用此方法,但出现异常:

NSString * LASTNAME = @"lastName";
NSString * GRADE = @"grade";

NSSortDescriptor *lastDescriptor =
[[NSSortDescriptor alloc]
initWithKey:LASTNAME
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)];

NSSortDescriptor *firstDescriptor =
[[NSSortDescriptor alloc]
initWithKey:GRADE
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)]];

NSArray * descriptors =
[NSArray arrayWithObjects:lastDescriptor, firstDescriptor, nil];
NSArray * sortedArray =
[students sortedArrayUsingDescriptors:descriptors];

@interface Student : NSObject
{
NSString * firstName;
NSString * lastName;
double grade;
}

最佳答案

NSNumber 不响应 localizedCaseInsensitiveCompare:。您只需要简单的 compare: 作为数字。

关于objective-c - 根据 double 值对包含对象的 NSmutable 数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10646532/

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