简体   繁体   English

根据前几个字符串按字母顺序排序数组

[英]alphabetically sort array based on first few strings

I woud like to sort an array which typically includes names and email addresses. 我想对通常包含名称和电子邮件地址的数组进行排序。 The problem is that the email addresses appear last even though they may start with 'a' 问题是,即使电子邮件地址可能以“ a”开头,它们也显示在最后

eg 例如

$myarray = ("Alex Mayfeild", "David Beckham", "Oliver Twist", "ant.stev@wherever.com", "peter.pan@neverland.com", ........) //and so on

Upon sorting the array using php's sort function "ant.stev@wherever.com" will appear close to the end even though the functionality I would like to achieve is for him to appear after Alex. 在使用php的排序功能“ ant.stev@wherever.com”对数组进行排序后,即使我想要实现的功能是在Alex之后出现,它也将接近尾声。

natcasesort and natsource functions based on natural ordering seem to fail. 基于自然顺序的natcasesort和natsource函数似乎失败。 Correction: natcasesource works it returns true when working as stated in docs. 更正:natcasesource的工作原理是按文档所述返回true。 Thanks @meagar 谢谢@meagar

Is there anyway to achieve the requested functionality. 反正有实现所需功能的方法。 Thanks for any help guys. 感谢您的帮助。 It is very much appreciated. 这是非常赞赏。

sort() is case sensitive, as it sorts based on the letters ASCII value. sort()区分大小写,因为它基于字母ASCII值进行排序。

Try natcasesort() , if you want too "sort an array using a case insensitive 'natural order' algorithm". 如果您也想“使用不区分大小写的“自然顺序”算法对数组进行排序”,请尝试natcasesort()

Seems to me that sort($myarray, SORT_STRING|SORT_FLAG_CASE); 在我看来, sort($myarray, SORT_STRING|SORT_FLAG_CASE); should sort the array the way you want. 应该按照您想要的方式对数组进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM