按字典键排序可变数组

ZeN*_*ewb 6 sorting iphone objective-c nsmutablearray ios

我已经使用各种排序方法查看了几个答案NSMutableArray,但由于某种原因它们不适合我.

我只是试图通过Delay每个字典中的键对包含字典的可变数组进行排序.但是,"sorted"数组与原始数组完全相同.

顺便说一句,如果我创建一个虚拟可变数组并用包含数字的字典填充它,它可以正常工作,但由于某种原因它不会对我正在初始化的这个可变数组进行排序.

我究竟做错了什么?

这是我的代码:

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);
Run Code Online (Sandbox Code Playgroud)

这是包含字典的数组:

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;
    }
)
Run Code Online (Sandbox Code Playgroud)

Joe*_*Joe 8

当我NSNumber在字典中使用s 时,上面的代码很好.这让我相信这个Delay值在字典中存储为字符串.你需要做的是按字符串排序integerValue.

NSSortDescriptor *delay = 
   [NSSortDescriptor sortDescriptorWithKey:@"Delay.integerValue" ascending:YES];
Run Code Online (Sandbox Code Playgroud)