[英]How to sort NSArray with dictionary objects alphabetically?
我有未排序的字典數組,我想根據藝術家姓名對其進行排序。未排序的列表如下:
{
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;
}
但它仍然沒有完全排序。 知道如何使用藝術家姓名按字母順序對其進行排序。請幫助我!
像這樣為每個字典值使用 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]]);
}
最后你會得到你現在正在尋找的東西..享受這個編碼..
如果您有任何疑問,請隨時詢問。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.