0
$a=array('a'=>'`a:1:`','b'=>'`a:`','c'=>'`a:0:`');
arsort($a);
print_r($a);

I expect this code to output

Array
(
    [a] => `a:1:`
    [c] => `a:0:`
    [b] => `a:`
)

but it actually outputs

Array
(
    [b] => `a:`
    [a] => `a:1:`
    [c] => `a:0:`
)

Do you understand why the backticks are messing up ?

guigoz
  • 674
  • 4
  • 21
  • The shorter string wins, no surprises here. – raina77ow Mar 16 '18 at 14:57
  • 3
    The backtick will be considered as character for sorting. As all start with one that is not an isuse but the length of strings is different so the last backtick is considered in your case. See example until after the `:` all values are the same, so next char is backtick, 1 and 0, and those are ordered reverse, which seesm correct to me. – ChristianM Mar 16 '18 at 14:57
  • oh that's damn right, I kept on considering the backticks as not part of the strings – guigoz Mar 16 '18 at 15:02
  • Please consider marking this as answered if it solves your problem - https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Nigel Ren Mar 16 '18 at 15:38

1 Answers1

1

The backtick will be considered as character for sorting. As all start with one that is not an isuse but the length of strings is different so the last backtick is considered in your case. See example data, until after the : all values are the same, so the next chars to sort by are backtick, 1 and 0, and those are ordered reverse, which seems correct to me.

Giving my comment from above as answer, because I guess it's the right answer.

To extend it, you might need to do a uasort using trim to remove the backticks and sorting reversely then.

ChristianM
  • 1,793
  • 12
  • 23