8

strnatcmp在比较函数中使用对表格中的人名进行排序。对于我们的比利时客户,我们得到了一些奇怪的结果。他们有像“范德布罗克”和“范德维尔”这样的名字,然后strnatcasecmp("Van der", "Vander")回来了0

由于自然比较旨在像人类一样进行排序,我不明白为什么这些空间会被完全忽略。

例如:

$names = array("Van de broecke", "Vander Veere", "Vande Muizen", "Vander Zoeker", "Van der Programma", "vande Huizen", "vande Kluizen", "vander Muizen", "Van der Luizen");
natcasesort($names);

print_r($names);

给出:

Array ( 
[0] => Van de broecke 
[5] => vande Huizen 
[6] => vande Kluizen 
[2] => Vande Muizen 
[8] => Van der Luizen 
[7] => vander Muizen 
[4] => Van der Programma 
[1] => Vander Veere 
[3] => Vander Zoeker 
)

但是人类会说:

Array ( 
[0] => Van de broecke 
[4] => Van der Programma 
[8] => Van der Luizen 
[5] => vande Huizen 
[6] => vande Kluizen 
[2] => Vande Muizen 
[7] => vander Muizen 
[1] => Vander Veere 
[3] => Vander Zoeker 
)

我现在的解决方案是将所有空格替换为下划线,处理得当。两个问题:为什么会这样natsort工作?有更好的解决方案吗?

4

3 回答 3

2

If you look in the source code you can actually see this, which definitely seems like a bug: http://gcov.php.net/PHP_5_3/lcov_html/ext/standard/strnatcmp.c.gcov.php (scroll down to line 130):

 //inside a while loop...

 /* Skip consecutive whitespace */
 while (isspace((int)(unsigned char)ca)) {
         ca = *++ap;
 }

 while (isspace((int)(unsigned char)cb)) {
         cb = *++bp;
 }

Note that's a link to 5.3, but the same code still exists in 5.5 (http://gcov.php.net/PHP_5_5/lcov_html/ext/standard/strnatcmp.c.gcov.php) Admittedly my knowledge of C is limited, but this basically appears to be advancing the pointer on each string if the current character is a space, essentially ignoring that character in the sort. The comment implies that it's only doing this if the whitespaces are consecutive; however, there is no check to ensure the previous character was actually a space first. That would need something like

//declare these outside the loop
short prevAIsSpace = 0;
short prevBIsSpace = 0;

//....in the loop
while (prevAIsSpace && isspace((int)(unsigned char)ca)) {
    //won't get here the first time since prevAIsSpace == 0
    ca = *++ap;
}
//now if the character is a space, flag it for the next iteration
prevAIsSpace = isspace((int)(unsigned char)ca));
//repeat with string b
while (prevBIsSpace && isspace((int)(unsigned char)cb)) {
    cb = *++bp;
}
prevBIsSpace = isspace((int)(unsigned char)cb));

Someone who actually knows C could probably write this better, but that's the general idea.

On another potentially interesting note, for your example, if you're using PHP >= 5.4, this gives the same result as the usort mentioned by Aaron Saray (it does lose the key/value associations as well):

sort($names, SORT_FLAG_CASE | SORT_STRING);

print_r($names);
Array ( 
    [0] => Van de broecke 
    [1] => Van der Luizen 
    [2] => Van der Programma 
    [3] => vande Huizen 
    [4] => vande Kluizen 
    [5] => Vande Muizen 
    [6] => vander Muizen 
    [7] => Vander Veere 
    [8] => Vander Zoeker
) 
于 2013-08-24T23:35:43.513 回答
2

查看 bugs.php.net #26412(natsort() 将多个空格压缩为 1 个空格)。显然,这种行为是如此“aa”、“a a”和“a a”(注意 2 个空格)不会作为相同的字符串排序。

于 2013-08-21T17:15:27.923 回答
2

就像其他答案/评论者所说的那样,这是一个已知问题。但是,您可以使用 usort() 编写自己的排序。请试试这个,看看它是否有效:

usort($names2, function($first, $second) {
    if ($first == $second) {
        return 0;
    }
    else {
        return (strtolower($first) < strtolower($second)) ? -1 : 1;
}
});

我注意到输出与您建议的答案略有不同:

你建议:

[4] => Van der Programma 
[8] => Van der Luizen

但我确定这是一个错字 - 这些应该被交换。:)

于 2013-08-23T19:32:10.887 回答