I'm using strnatcmp
in my comparison function for sorting person names in a table. For our Belgian client, we get some strange results. They have names like 'Van der Broecke' and 'Vander Veere', and strnatcasecmp("Van der", "Vander")
returns 0
!
As natural comparison aims to sort as a human would, I don't understand why the spaces are completely disregarded.
E.g.:
$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);
Gives:
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
)
But a human would say:
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
)
My solution now is to replace all spaces with underscores, which are handled properly. Two questions:
Why does natsort
work like this?
Is there a better solution?
natsort
sorts strings by numbers as humans do, it doesn't sort correctly if there's no numbers – Lamrert