1

我有一组要排序的电子邮件地址,但结果很奇怪。这就是我的意思:

sort($array);
print_r($array);
...[79] => 91******@******.com [80] => 9l***@**********.com 
[81] => ps*******@**********.com [82] => a.c******@*****.com 
[83] => a.d****@*****.com...

什么会导致以 p 开头的电子邮件在数字之后和 A 之前混在一起?

我从数据库中删除了该电子邮件地址并将其替换为“测试”,然后“测试”出现在同一位置。

4

3 回答 3

1

To make sure you're comparing in English put this before your sort:

  setlocale (LC_COLLATE, 'en_US');
  sort($array);

If this changes anything and the sort works, your system is set to compare strings not as US/English.

Other than that, I can only think of spaces being at the start of the string or some starting with upper case and others starting lower. To fix cased string sorting issues, you could do:

  natsort($array);
于 2012-11-03T22:38:37.747 回答
0

maybe you are sorting with some weird rule. Force the sorting mode to "string" passing SORT_STRING

sort($array, SORT_STRING);

and tell us what it spit out.

sort() uses SORT_REGULAR as default, I suspect that the emails with numeric prefixes (92*) are considered integers thus the weird sorting.

于 2012-11-03T22:58:35.020 回答
0

You have a space before ps ... when I inspected the code you posted it is visible. Make a trim before sorting.

于 2012-11-03T22:48:13.667 回答