我正在尝试在iPhone应用程序中按Aa-Zz格式对NSMutablArray值进行排序。我在项目中使用了以下代码。
for (id key in [dictionary allKeys])
{
id value = [dictionary valueForKey:key];
[duplicateArray addObject:value];
[value release];
value =nil;
NSSortDescriptor *sortdis = [[[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES] autorelease];
NSArray *discriptor = [NSArray arrayWithObjects:sortdis,nil];
mutableArray = [[duplicateArray sortedArrayUsingDescriptors:discriptor]retain];
}
NSLog(@"mutableArray : %@", mutableArray);
[mutableArray sortUsingSelector:@selector(caseInsensitiveCompare:)];
应用程序因以下错误日志而崩溃:
[__NSArrayI sortUsingSelector:]: unrecognized selector sent to instance.
但是,我已经搜索并找到了崩溃的原因。我的mutableArray值是这样的,
(
{
Id = 00001;
Name = "ABCD";
},
{
Id = 00002;
Name = "BCDE";
},
{
Id = 00003;
Name = "ZXYS";
},
{
Id = 00004;
Name = "abcd";
},
{
Id = 00009;
Name = "bcde";
},
)
我想对这个数组值进行排序,例如ABCD,abcd,BCDE,bcde,ZXYs。
谁能帮忙解决这个问题。
最佳答案
戈皮纳斯。请在您的项目中使用此更新的代码。
for (id key in [dictionary allKeys])
{
id value = [dictionary valueForKey:key];
[duplicateArray addObject:value];
[value release];
value =nil;
NSSortDescriptor *sortdis = [[[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]autorelease];
NSArray *discriptor = [NSArray arrayWithObjects:sortdis,nil];
mutableArray = [[duplicateArray sortedArrayUsingDescriptors:discriptor]retain];
}
NSLog(@"mutableArray : %@", mutableArray);
希望对您有帮助。谢谢。