Sort the friends list by pinyin

Source: Internet
Author: User
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 ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.