2012-04-10 97 views
5

我有無序的字典數組,我想根據藝術家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; 
     } 

但仍是不完全排序。任何想法如何使用藝術家名字按字母順序排序。請幫助我!

+5

排序有什麼問題?你顯示的結果是爲了?你期望他們在什麼順序? – 2012-04-10 06:21:09

+0

可能的重複[如何排序包含NSDictionaries的NSArray?](http://stackoverflow.com/questions/3361207/how-can-i-sort-an-nsarray-containing-nsdictionaries) – 2012-04-10 06:22:00

+0

是您的具體問題這種排序是區分大小寫的?如果是這樣,你需要'[NSSortDescriptor sortDescriptorWithKey:@「artist」ascending:YES selector:@selector(caseInsensitiveCompare :)]'。 – Dondragmer 2012-04-10 06:22:00

回答

-1

使用對於這樣

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