Multidimensional array sort with UTF8 collator locale special characters

The ‘correct’ alphabetical sorting is highly dependent on the locale.  Some countries sort the special characters to the end and some not.  A variety of so called solutions were found on the web, but few were clean enough and covered my special case.  A generic multidimensional array needed to be sorted by a realtime set of fields with variable sort directions.

A combination of  a uasort with a user defined comparision function that used php’s collator class saved the day.  A swap routine helped achieve any change in sort direction.

Do please let me know if there is a cleaner more efficient way to sort multidimensional arrays by user defined columns with user defined sort directions in wordpress for a locale defined by wordpress.

Multidimensional array sort with UTF8 collator locale special characters
Test data sorted under different locales

I had to resort to globals to pass the ‘realtime’ sortfields and the locale (array(field1=>sortdirection, etc).

The quick test function was:

function do_sort(&$arr, $locale, $sortfields) {
global $collator, $sortfields;
$collator = new collator($locale);
if (empty($sortfields or !is_array($sortfields))) return $arr;
uasort ($arr, function ( $a, $b) {
global $collator, $sortfields;
foreach ($sortfields as $fld1 => $sort1) {
// really only until return!
if (empty ($a[$fld1])) $aa = '';
else $aa = $a[$fld1];
if (empty ($b[$fld1])) $bb = '';
else $bb = $b[$fld1];
if ($sort1 == SORT_DESC) amr_swap($aa, $bb);
$cmp = $collator->compare($aa, $bb);
if (!($cmp == 0)) return $cmp;
return $cmp;
return $arr;

The test code is provided in a test_utf8_sort2 Zip for your fun.

Some reference links noted during the process:

  • custom alphabet function:
  • use db rather with unicode:

For wordpress queries, what was not working in the wp users screen sortable columns, did work when queried directly:

SELECT * FROM `beta_usermeta` WHERE `meta_key` = "first_name" ORDER BY `meta_value` COLLATE utf8mb4_unicode_520_ci


Dead shortcodes and how to find them

Have you ever looked at your plugin list and wondered whether you actually using it anywhere?  Or perhaps deactivated a plugin or swopped themes without realising that you  now have a dead shortcode on a page or post somewhere telling the world that you really aren’t on  top of things in your websites.

Yes well, it happens.  This little plugin may help.

amr-shortcodes searchs through your published or future posts, pages etc looking for shortcodes.  It then lists the pages and the shortcodes that appear on them, with handly little links to edit the post.  It will also flag with a red cross if wordpress does not know about that shortcode anymore – ie the shortcode is dead and there is no function that will switch out the shortcode text for some wonderful other text.

It does not automatically delete the dead text, because you reallly should look at the page or post and delete any surrounding text as well that may no longer be relevant.

And then, just because it might be interesting, I added a tab so you can see the available shortcodes.

View and Manage shortcodes