Iphone 按字典键排序可变数组
我已经用Iphone 按字典键排序可变数组,iphone,objective-c,ios,sorting,nsmutablearray,Iphone,Objective C,Ios,Sorting,Nsmutablearray,我已经用NSMutableArray的各种排序方法浏览了一些答案,但由于某些原因,它们对我不起作用 我只是尝试按照每个字典中的Delay键对包含字典的可变数组进行排序。但是,“排序”数组与原始数组完全相同 顺便说一句,如果我创建一个虚拟可变数组并用包含数字的字典填充它,它可以正常工作,但是由于某种原因,它不会对我正在初始化的可变数组进行排序 我做错了什么 这是我的密码: playlistCalls = [[NSMutableArray alloc] initWithArray:[currentP
NSMutableArray
的各种排序方法浏览了一些答案,但由于某些原因,它们对我不起作用
我只是尝试按照每个字典中的Delay
键对包含字典的可变数组进行排序。但是,“排序”数组与原始数组完全相同
顺便说一句,如果我创建一个虚拟可变数组并用包含数字的字典填充它,它可以正常工作,但是由于某种原因,它不会对我正在初始化的可变数组进行排序
我做错了什么
这是我的密码:
playlistCalls = [[NSMutableArray alloc] initWithArray:[currentPlaylist objectForKey:@"Tunes"]];
NSLog(@"original %@", playlistCalls);
NSSortDescriptor *delay = [NSSortDescriptor sortDescriptorWithKey:@"Delay" ascending:YES];
[playlistCalls sortUsingDescriptors:[NSArray arrayWithObject:delay]];
NSLog(@"sorted %@", playlistCalls);
以下是包含字典的数组:
2012-06-04 15:48:09.129 MyApp[57043:f503] original (
{
Name = Test Tune;
Delay = 120;
Volume = 100;
},
{
Name = Testes;
Delay = 180;
Volume = 100;
},
{
Name = Testing;
Delay = 60;
Volume = 100;
}
)
2012-06-04 15:48:09.129 MyApp[57043:f503] sorted (
{
Name = Test Tune;
Delay = 120;
Volume = 100;
},
{
Name = Testes;
Delay = 180;
Volume = 100;
},
{
Name = Testing;
Delay = 60;
Volume = 100;
}
)
请尝试以下方法,它对我有效
NSDictionary *dic;
NSMutableArray *arr = [[NSMutableArray alloc] init];
dic = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:120], @"Delay",
[NSNumber numberWithInt:100], @"Volume",
nil];
[arr addObject:dic];
dic = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:160], @"Delay",
[NSNumber numberWithInt:100], @"Volume",
nil];
[arr addObject:dic];
dic = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:100], @"Delay",
[NSNumber numberWithInt:100], @"Volume",
nil];
[arr addObject:dic];
NSSortDescriptor *sortDesc = [[NSSortDescriptor alloc] initWithKey:@"Delay" ascending:YES];
[arr sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];
当我在字典中使用
NSNumber
s时,上面的代码很好。这使我相信,Delay
值在字典中存储为字符串。然后需要做的是按字符串排序integerValue
NSSortDescriptor *delay =
[NSSortDescriptor sortDescriptorWithKey:@"Delay.integerValue" ascending:YES];
当您初始化可变数组并添加类似的字典时,它会起作用,但是当我已经有一个预填充的可变数组时,为什么它不适用于我的情况呢?这就是我一直坚持的:\谢谢,但我是个傀儡,把它们存储为字符串。乔抓住它,给了我一个补丁。还是谢谢你!对,你是。。。这就成功了。我需要回去把它修好。谢谢