按字母排序php,php实现姓名根据首字母排序的类与方法(实例代码)

PHP将名字按首字母进行排序,具体代码如下所示:

PHP;">

128) { //汉字开头,汉字没有以U、V开头的

$asc=ord($s{0})*256+ord($s{1})-65536;

if($asc>=-20319 and $asc<=-20284)return "A";

if($asc>=-20283 and $asc<=-19776)return "B";

if($asc>=-19775 and $asc<=-19219)return "C";

if($asc>=-19218 and $asc<=-18711)return "D";

if($asc>=-18710 and $asc<=-18527)return "E";

if($asc>=-18526 and $asc<=-18240)return "F";

if($asc>=-18239 and $asc<=-17760)return "G";

if($asc>=-17759 and $asc<=-17248)return "H";

if($asc>=-17247 and $asc<=-17418)return "I";

if($asc>=-17417 and $asc<=-16475)return "J";

if($asc>=-16474 and $asc<=-16213)return "K";

if($asc>=-16212 and $asc<=-15641)return "L";

if($asc>=-15640 and $asc<=-15166)return "M";

if($asc>=-15165 and $asc<=-14923)return "N";

if($asc>=-14922 and $asc<=-14915)return "O";

if($asc>=-14914 and $asc<=-14631)return "P";

if($asc>=-14630 and $asc<=-14150)return "Q";

if($asc>=-14149 and $asc<=-14091)return "R";

if($asc>=-14090 and $asc<=-13319)return "S";

if($asc>=-13318 and $asc<=-12839)return "T";

if($asc>=-12838 and $asc<=-12557)return "W";

if($asc>=-12556 and $asc<=-11848)return "X";

if($asc>=-11847 and $asc<=-11056)return "Y";

if($asc>=-11055 and $asc<=-10247)return "Z";

}else if(ord($s)>=48 and ord($s)@H_404_27@=65 and ord($s)<=90){ //大写英文开头

return substr($s,1);

}else if(ord($s)>=97 and ord($s)<=122){ //小写英文开头

return strtoupper(substr($s,1));

}

else

{

return iconv_substr($s0,'utf-8');

//中英混合的词语,不适合上面的各种情况,因此直接提取首个字符即可

}

}

public function addPeople()

{

$userName = array('张三','马大帅','李四','王五','小二','猫蛋','狗蛋','王花','三毛','小明','李刚','张飞');

sort($userName);

foreach($userName as $name){

$char = $this->getFirstChar($name);

$nameArray = array();

if(count($charArray[$char])!=0){

$nameArray = $charArray[$char];

}

array_push($nameArray,$name);

$charArray[$char] = $nameArray;

}

ksort($charArray);

$this->assign('_list',$charArray);

}

PHP;">

{$k}

总结

以上所述是小编给大家介绍的PHP实现姓名根据首字母排序的类与方法,希望对大家有所帮助。程序员遇到问题都会上(编程之家jb51.cc)查找问题解答方法!如果觉得站点还不错,随手转发给程序员朋友一下!

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现按中文首字母排序可以使用拼音库,Java中常用的拼音库有pinyin4j和tinyPinyin。 下面是使用pinyin4j实现按中文首字母排序的示例代码: ```java import java.util.*; import net.sourceforge.pinyin4j.PinyinHelper; public class ChineseSorter { public static void main(String[] args) { String[] names = {"张三", "李四", "王五", "赵六", "陈七", "钱八"}; List<String> list = Arrays.asList(names); Collections.sort(list, new ChineseComparator()); System.out.println(list); } static class ChineseComparator implements Comparator<String> { @Override public int compare(String o1, String o2) { String[] pinyin1 = PinyinHelper.toHanyuPinyinStringArray(o1.charAt(0)); String[] pinyin2 = PinyinHelper.toHanyuPinyinStringArray(o2.charAt(0)); if (pinyin1 != null && pinyin2 != null) { int cmp = pinyin1[0].compareTo(pinyin2[0]); if (cmp == 0) { return o1.compareTo(o2); } else { return cmp; } } else { return o1.compareTo(o2); } } } } ``` 运行结果: ``` [陈七, 李四, 钱八, 张三, 赵六, 王五] ``` 这里使用了内部实现了Comparator接口,实现compare方法来指定排序规则。在compare方法中,首先将中文转换为拼音数组,然后比较拼音数组的第一个拼音,如果相同就比较原始字符串,否则按照拼音排序。 需要注意的是,pinyin4j只能转换简体中文,对于繁体中文可能存在一些问题。如果需要支持繁体中文,可以考虑使用其他拼音库,比如tinyPinyin。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值