19

PHP 5.4 introduces the useful SORT_FLAG_CASE for making any other search case insensitive. Unfortunately this isn't available in PHP 5.3 or less and so I was wondering how the following array:

array('a'=>2,'b'=>4,'A'=>1,'B'=>3);

Could be sorted into:

array('A'=>1,'a'=>2,'B'=>3,'b'=>4);

As the usual ksort() function sorts it as:

array('A'=>1,'B'=>3,'a'=>2,'b'=>4);
2
  • I can't accept it until 2 days after posting. Commented Aug 30, 2012 at 11:05
  • Right, I forgot about that, so please feel reminded in one+ days ;) Commented Aug 30, 2012 at 11:07

3 Answers 3

41

A comment on one of the PHP function reference pages pointed me to the uksort() function; this (and the uasort() function for sorting by value instead of key) allow the comparison algorithm for shifting in the quick sort to be written by the user.

Combine this with the very simple strcasecmp() function (which compares two strings and returns <0 for a>b and >0 for a>b) gives you:

uksort($array, 'strcasecmp');

To easily achieve the effect of:

ksort($array,SORT_STRING | SORT_FLAG_CASE);

In PHP 5.3 or less.

Sign up to request clarification or add additional context in comments.

6 Comments

How can php have such a screwed up sorting system? And according to the docs: sort($words, SORT_STRING | SORT_FLAG_CASE); should do a case insensitive sort of the values, but it doesn't work:
That parameter will work but only in PHP 5.4. PHP has a lot of flaws from its age and usage but more recent updates are really focussing on tightening up the consistency of the language and provide features that other languages are used to.
how would one change uksort($array, 'strcasecmp'); to sort in reverse order?
You can reverse an array order with array_reverse($arr). Pass in true as the 2nd parameter if you want to preserve numeric keys (non-numeric keys are always preserved, and as that's what this question deals with it's probably not necessary).
krsort($array, SORT_NATURAL | SORT_FLAG_CASE); can be rewritten in this way: uksort($array, function ($a, $b) { return strcasecmp($b, $a); }); (PHP 5.3 support).
|
2

This is how I have sorted (case insensitive) array of utf8 strings in PHP 7:

uksort($myarray, function ($a, $b) {
  $a = mb_strtolower($a);
  $b = mb_strtolower($b);
  return strcmp($a, $b);
});

For PHP 5.3 you need to create your own sort function:

function mySort1($a, $b) {
  $a = mb_strtolower($a);
  $b = mb_strtolower($b);
  return strcmp($a, $b);
}
uksort($result['rows'], 'mySort1');

Comments

0

I suggest, to use SORT_NATURAL flag to prevent unnatural sorting when array keys contain number or are numbers:

ksort($elements, SORT_NATURAL | SORT_FLAG_CASE);

Standard sorting:

Array
(
   img1.png => some_data_1
   img10.png => some_data_10
   img12.png => some_data_12
   img2.png => some_data_2
)

Natural order sorting:

Array
(
    img1.png => some_data_1
    img2.png => some_data_2
    img10.png => some_data_10
    img12.png => some_data_12
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.