user1294886
user1294886

Reputation: 87

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'

e.g.

$myarray = ("Alex Mayfeild", "David Beckham", "Oliver Twist", "[email protected]", "[email protected]", ........) //and so on

Upon sorting the array using php's sort function "[email protected]" will appear close to the end even though the functionality I would like to achieve is for him to appear after Alex.

natcasesort and natsource functions based on natural ordering seem to fail. Correction: natcasesource works it returns true when working as stated in docs. Thanks @meagar

Is there anyway to achieve the requested functionality. Thanks for any help guys. It is very much appreciated.

Upvotes: 1

Views: 330

Answers (2)

user229044
user229044

Reputation: 239240

sort() is case sensitive, as it sorts based on the letters ASCII value.

Try natcasesort(), if you want too "sort an array using a case insensitive 'natural order' algorithm".

Upvotes: 2

KRyan
KRyan

Reputation: 7598

Seems to me that sort($myarray, SORT_STRING|SORT_FLAG_CASE); should sort the array the way you want.

Upvotes: -1

Related Questions