[Tiki-devel] A Unicode-aware replacement for the natcasesort() function

Volker Wysk <post-hhF2Jplw28UoZk/[email protected]>
Newsgroups gmane.comp.cms.tiki.devel
Message-ID <[email protected]>
Hi!

Here it is:

----------------------------------------------------------------------------
// Do a natural, locale aware sort of an array of strings.
//
// "Natural" means that numbers are sorted by their value, not by the digits
// they contain. For instance, "10" is ordered after "2".
//
// The "lang" argument is the language for which to order. Accented
// characters, like "ä", are ordered with their non-accented counterpart, 
// like "a". In the non-locale aware natcasesort() function, accented 
// characters get ordered at the end. That's not right for non-English 
// languages.
//
// For the "lang" argument, "false" can be specified. In this case, the 
// function investigates the language of the current user and uses that.
//
// mb_natcasesort() depends on the "intl" PHP extension.

function mb_natcasesort(string $lang, array &$array): bool
{
    if ($lang == false) {
        $tikilib  = TikiLib::lib('tiki');
        $loginlib = TikiLib::lib('login');

        $user = $loginlib->getUser();
        $lang = $tikilib->get_language($user);
    }

    $coll = collator_create($lang);
    collator_set_attribute($coll, Collator::NUMERIC_COLLATION, 
                           Collator::ON);
    collator_set_attribute($coll, Collator::CASE_FIRST, 
                           Collator::LOWER_FIRST);
    collator_set_attribute($coll, Collator::ALTERNATE_HANDLING, 
                           Collator::SHIFTED);

    return collator_asort($coll, $array);
}
----------------------------------------------------------------------------

Any comments are welcome.

Regards,
Volker

_______________________________________________
TikiWiki-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tikiwiki-devel
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEE6QXGh82Ov3+2nrxp+K4ydFOsHoUFAmSn968ACgkQ+K4ydFOs
HoUY6RAA3LpM1bi08w9Kx1PqYYDbp92bfvsFmCxZQ1jX05ofohz+Q80PJwwiRTrC
mMgQYsAslmfKtR3/MKWjaaalH+oCHLZFbu8fJ0d14jgFPX4yOOhZEYlCF4wTDSLe
jn/8ACln8Z425GvY0t6agj2ej7osFG1EjtaKbsqZfHC0CgO5caEXGP4L9xYtsUk5
AjTfvwMrPy2/Rq8V14pZGxsPWq29nJILfoLv7fglczvlET1RQsDZc6egP6reJIZE
A6d9t3OqKNMj+rltdQxIZWzDZTzmRmrl10uO68MQ8edwB+KE0l4zvlI6KzvzZBwF
UPU8tEQxcE95mFk2em87ElU+y/EHPN97NWmwkeMKtcoFNfQo3R7zSoEWb+1kLdpo
p69hotro3XCZpDs4+UXmelV0jnGXl0XsRzG0nRGbOLqXuiixuFuaxnI08U8K9dzN
CuDQgH/Yj7gYv9H41zJnaEfj0jhFuRaVGzLF4skZwCHJzW9gJGJUZd+MOG8Mqdr+
bqmXEhu3byfZOhTvpMo/bGEXQSjOmuBvWknp5phEN8hBi3eebXGcW0Zff97DZwLS
GsrvPcQjsZprU1q+lkdtQmuL3O0YPKpMUwnHPGv6gD6MAAJ7xM/dA5pGWeBW08mF
HOcNnlEpw4NvI41hiiVE6RpT40+Lm4DRGDuRep/AjEjbQuvkGGQ=
=lukg
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.