As the social needs of applications increase, your applications cannot be added to the friend list. But what if there are too many friends? You need to sort the names of friends by pinyin. Next I will explain how to sort the dictionary of several friends in an array by name in pinyin:
[_ Friendobjectarray sortusingfunction: friendsortfuc context: Nil];
The above code calls a method named friendsortfuc to make _ friendobjectarray orderly.
The following is the implementation of this method:
Static nsinteger friendsortfuc (nsdictionary * onedic, nsdictionary * otherdic, void * context ){
Pinyin * pinyin = [pinyin getinstance];
Nsstring * onenamepinyin = [pinyin. Map objectforkey: [[onedic objectforkey: @ "user_name"] substringtoindex: 1];
Nsstring * othernamepinyin = [pinyin. Map objectforkey: [[otherdic objectforkey: @ "user_name"] substringtoindex: 1];
If (onenamepinyin = nil ){
Onenamepinyin = @"~ ";
}
If (othernamepinyin = nil ){
Othernamepinyin = @"~ ";
}
Return [onenamepinyin compare: othernamepinyin options: nsstringenumerationbywords];
}
The pinyin class in the code has been prepared by the predecessors. download from here:
Http://www.cocoachina.com/bbs/read.php? Tid-38851-page-1.html
Before using pinyin. map, you need to load the pinyin table:
Pinyin * pinyin = [pinyin getinstance];
If (pinyin. Map = nil ){
[Pinyin loadmap];
}
In this way, _ friendobjectarray is sorted by the pinyin name! Easy to use ~