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 中的排序键获得排序字典

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

最后你会得到你现在正在寻找的东西..享受这个编码..
如果你有任何疑问,请随时问..

于 2012-04-10T06:38:40.650 回答