gpt4 book ai didi

iphone - 按字典键对可变数组进行排序

转载 作者:可可西里 更新时间:2023-11-01 03:44:20 28 4
gpt4 key购买 nike

我已经使用 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;
}
)

最佳答案

当我在字典中使用 NSNumber 时,上面的代码没有问题。这使我相信 Delay 值在您的字典中存储为字符串。然后您需要做的是按字符串 integerValue 排序。

NSSortDescriptor *delay = 
[NSSortDescriptor sortDescriptorWithKey:@"Delay.integerValue" ascending:YES];

关于iphone - 按字典键对可变数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10887490/

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