How to sort considering special characters?

Posted on 2005-05-12
Last Modified: 2012-05-05
Hi Everybody,

When i sort an array (or something else in PHP), it considers default Latin sorting from a to z. But i want to perform sorting considering special characters such as ü,ğ,i,ş,ç,ö,é, etc.

Should i change a setting in php.ini? or can i perform this by inserting a line into my PHP script?

Question by:blacklord
    LVL 5

    Accepted Solution

    First use the setlocale() function.
    Here's a list of locale strings you might use.

    i.e. setlocale(LC_COLLATE, "german");

    Then use the sort() function with the SORT_LOCALE_STRING flag.

    i.e. sort(myArray, SORT_LOCALE_STRING);

    Hope that helps,
    LVL 5

    Expert Comment

    However, sort() won't recognize special entities like ğ and ş.  You'll need to translate those into their actual characters before sorting.  If you need to do that, use html_entity_decode() first:

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now