Support » Developing with WordPress » wp_list_sort issue with cases

  • shazdeh

    (@shazdeh)


    wp_list_sort() is fantastic, so easy to sort anything. This function however when sorting alphabetically, takes letter cases into account, so if I have: [ S, s, a, A ], it comes out as: [ A, S, a, s ], uppercases always come first. I’m not sure if this is a bug or how it’s intended to work, but it’s not very useful this way. Any chance this could get fixed or possibly an option to change this behavior?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Howdy_McGee

    (@howdy_mcgee)

    The wp_list_sort() uses strcmp() which converts the string to binary for the comparison and in binary uppercase letters come before lowercase letters.

    WP_List_Util::sort() Docs
    WP_List_Util::sort_callback() Docs

    This is probably something you’ll have have to keep in mind but you’re more than welcome to raise a ticket on trac regarding the issue, I can’t think of why strcmp() can’t be replaced with strnatcasecmp()

    In the meantime if you’re just sorting in Ascending order you can use this answer on Stack Overflow

    $arr = array( 'A', 's', 'S', 'a' );
    usort( $arr, 'strnatcasecmp' );
    shazdeh

    (@shazdeh)

    Thank you for the solution, I have solved this on my end however was wondering if this is something WP needs to fix.
    This function is massively useful!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘wp_list_sort issue with cases’ is closed to new replies.