如何按字母顺序对字典对象进行NSArray排序?

时间:2012-04-10 06:19:06

标签: iphone objective-c ios

  

我有未排序的字典数组,我想根据艺术家的名字对其进行排序。未列出的列表如下:

            {
                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;
        }

但它仍未完全排序。任何想法如何使用艺术家名称按字母顺序排序。请帮助我!

1 个答案:

答案 0 :(得分:-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]]);
}

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