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

标签 iphone objective-c ios sorting nsmutablearray

我已经使用 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/

相关文章:

ios - 无法使用 MPMediaPickerController

iphone - 工具栏不会显示在键盘上方

ios - 如何使用 iPhone SDK 创建动态 UITableView 单元格

ios - QuickBlox iOS 问题 - 密码未验证

iphone - 如何在地址簿保存联系人前检查是否添加了组织名称?

ios - 链接器错误断言失败 : (atom->fixupCount() == 1)

iphone - Box2d SetLinearVelocity 不适用于重力

iphone - 创建自定义 UIButton 类

objective-c - 创建下拉列表

ios - Objective C 如何解析这个 NSString