如何按字母顺序对字典对象排序NSArray?
问题描述:
我有无序的字典数组,我想根据艺术家names.Unsorted列表进行排序它给出如下:如何按字母顺序对字典对象排序NSArray?
{
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(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:)];
在阵列现在你会得到整理键,使用此阵你可以通过使用排序的键在字母排序的dictionart
for(int i= 0;i<[dict count];i++)
{
NSLog("Test val:%@",[dict valueForKay:[keyArr objectAtIndex:i]]);
}
最后,你会得到你正在寻找什么现在..享受这个编码..
如果您有任何疑问随时问..
+0
这实际上不起作用,因为'KeyArr'的排序版本被丢弃。你可以使用'KeyArr = [KeyArr sortedArrayUsingSelector:@selector(caseInsensitiveCompare :)];'来存储它。但是,如果这是OP所需的,那么使用“artist”键和“caseInsensitiveCompare:”选择器排序现有数组的效率会更低。 – Dondragmer 2012-04-10 06:48:19
排序有什么问题?你显示的结果是为了?你期望他们在什么顺序? – 2012-04-10 06:21:09
可能的重复[如何排序包含NSDictionaries的NSArray?](http://stackoverflow.com/questions/3361207/how-can-i-sort-an-nsarray-containing-nsdictionaries) – 2012-04-10 06:22:00
是您的具体问题这种排序是区分大小写的?如果是这样,你需要'[NSSortDescriptor sortDescriptorWithKey:@“artist”ascending:YES selector:@selector(caseInsensitiveCompare :)]'。 – Dondragmer 2012-04-10 06:22:00