gpt4 book ai didi

iphone - 如何按字母顺序对带有字典对象的 NSArray 进行排序?

转载 作者:可可西里 更新时间:2023-11-01 05:00:34 35 4
gpt4 key购买 nike

I have unsorted dictionary array and I want to sort it according to artist names.Unsorted list are given below:

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

And after sorting using descriptor

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

it gives sorted array as

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

但仍然没有完全排序。知道如何使用艺术家姓名按字母顺序对其进行排序。请帮助我!

最佳答案

像这样为每个字典值使用 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]]);
}

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

关于iphone - 如何按字母顺序对带有字典对象的 NSArray 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10084010/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com