6

のさまざまな並べ替え方法を使用していくつかの回答をすでに調べました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;
    }
)
4

2 に答える 2

8

NSNumber辞書でsを使用する場合、上記のコードは問題ありません。そのため、Delay値は辞書に文字列として格納されていると思います。次に行う必要があるのは、文字列で並べ替えることintegerValueです。

NSSortDescriptor *delay = 
   [NSSortDescriptor sortDescriptorWithKey:@"Delay.integerValue" ascending:YES];
于 2012-06-04T20:09:54.430 に答える
1

次のことを試してください、それは私と一緒に働きました

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]];
于 2012-06-04T20:06:54.510 に答える