5

ソートされていない辞書配列があり、アーティスト名に従ってソートしたい.ソートされていないリストを以下に示します。

            {
                artist = "Afro Latin Jazz Orchestra";
                id = 10;
            },
                    {
                artist = "Avey, Bobby";
                id = 17;
            },
                    {
                artist = "American Symphony Orchestra";
                id = 32;
            },
                    {
                artist = "Akpan, Uwem";
                id = 97;
            },
                    {
                artist = "Austin, Ivy ";
                id = 123;
            },
                    {
                artist = "American Theatre Orchestra";
                id = 153;
            },
                    {
                artist = AudraRox;
                id = 171;
            },
                    {
                artist = "Atlas, James";
                id = 224;
            },
                    {
                artist = "Alden, Howard";
                id = 270;
            },
                    {
                artist = Astrograss;
                id = 307;
            },
                    {
                artist = "Adan, Victor";
                id = 315;
            },
                    {
                artist = "Alegria, Gabriel";
                id = 316;
            },
                    {
                artist = "Andersen, Kurt";
                id = 412;
            },
                    {
                artist = Antares;
                id = 420;
            },
                    {
                artist = "Austen, Jane ";
                id = 426;
            },
                    {
                artist = "Acuna, Claudia";
                id = 443;
            },
                    {
                artist = "Akinmusire, Ambrose";
                id = 444;
            },
                    {
                artist = "Anderson, Laurie Halse";
                id = 559;
            },
                    {
                artist = "Alvarez, Javier";
                id = 591;
            },
                    {
                artist = "Alexander, Jane";
                id = 674;
            },
                    {
                artist = "Andy Teirstein";
                id = 695;
            },
                    {
                artist = "Afro-Cuban Jazz Saxtet";
                id = 707;
            },
                    {
                artist = "Aurora ";
                id = 708;
            },
                    {
                artist = "Aurora ";
                id = 709;
            },
                    {
                artist = "August, Gregg ";
                id = 715;
            },
                    {
                artist = "Aldous, Brian";
                id = 777;
            },
                    {
                artist = "Anne Enright";
                id = 1130;
            }

そして、記述子を使用してソートした後

NSSortDescriptor *sortByName = [NSSortDescriptor sortDescriptorWithKey:@"artist" ascending:YES];
 NSArray *sortDescriptors = [NSArray arrayWithObject:sortByName];
 sortedArray = [artistArray sortedArrayUsingDescriptors:sortDescriptors];

ソートされた配列を次のように与えます

        {
            artist = "Acuna, Claudia";
            id = 443;
        },
            {
            artist = "Adan, Victor";
            id = 315;
        },
            {
            artist = "Afro Latin Jazz Orchestra";
            id = 10;
        },
            {
            artist = "Afro-Cuban Jazz Saxtet";
            id = 707;
        },
            {
            artist = "Akinmusire, Ambrose";
            id = 444;
        },
            {
            artist = "Akpan, Uwem";
            id = 97;
        },
            {
            artist = "Alden, Howard";
            id = 270;
        },
            {
            artist = "Aldous, Brian";
            id = 777;
        },
            {
            artist = "Alegria, Gabriel";
            id = 316;
        },
            {
            artist = "Alexander, Jane";
            id = 674;
        },
            {
            artist = "Alvarez, Javier";
            id = 591;
        },
            {
            artist = "American Symphony Orchestra";
            id = 32;
        },
            {
            artist = "American Theatre Orchestra";
            id = 153;
        },
            {
            artist = "Andersen, Kurt";
            id = 412;
        },
            {
            artist = "Anderson, Laurie Halse";
            id = 559;
        },
            {
            artist = "Andy Teirstein";
            id = 695;
        },
            {
            artist = "Anne Enright";
            id = 1130;
        },
            {
            artist = Antares;
            id = 420;
        },
            {
            artist = Astrograss;
            id = 307;
        },
            {
            artist = "Atlas, James";
            id = 224;
        },
            {
            artist = AudraRox;
            id = 171;
        },
            {
            artist = "August, Gregg ";
            id = 715;
        },
            {
            artist = "Aurora ";
            id = 708;
        },
            {
            artist = "Aurora ";
            id = 709;
        },
            {
            artist = "Austen, Jane ";
            id = 426;
        },
            {
            artist = "Austin, Ivy ";
            id = 123;
        },
            {
            artist = "Avey, Bobby";
            id = 17;
        }

しかし、まだ完全にはソートされていません。アーティスト名を使用してアルファベット順に並べ替える方法を教えてください。助けてください!

4

1 に答える 1

-1

このようなすべての辞書の値にforループセットキーを使用する

for(int i =0;i<[Arr count];i++)
{
   [dict setValue:[Arr objectAtIndex:i] forKey:[[Arr objectAtIndex:i] valueForKey:artist]];
}

今、あなたはこのようになります

Acuna, Claudia = {
    artist = "Acuna, Claudia";
    id = 443;
}       
Adan, Victor =  {
    artist = "Adan, Victor";
    id = 315;
}       
Afro Latin Jazz Orchestra =   {
    artist = "Afro Latin Jazz Orchestra";
    id = 10;
}       
Afro-Cuban Jazz Saxtet   {
    artist = "Afro-Cuban Jazz Saxtet";
    id = 707;
}       
Akinmusire, Ambrose =   {
    artist = "Akinmusire, Ambrose";
    id = 444;
}       

NSArray *KeyArr = [dict allKeys];

[KeyArr sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

これで、配列でソートされたキーを取得できます。この配列を使用すると、alphapet でソートされたキーを使用して、ソートされた dictionart を取得できます。

for(int i= 0;i<[dict count];i++)
{
     NSLog("Test val:%@",[dict valueForKay:[keyArr objectAtIndex:i]]);
}

最後に、あなたが今探しているものを手に入れるでしょう..このコーディングを楽しんでください..
疑問がある場合は、お気軽にお尋ねください..

于 2012-04-10T06:38:40.650 に答える