[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

How to sort considering special characters?

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?

  • 2
1 Solution
First use the setlocale() function.  http://us2.php.net/function.setlocale
Here's a list of locale strings you might use.  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_language_strings.asp

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

Then use the sort() function with the SORT_LOCALE_STRING flag.  http://us2.php.net/manual/en/function.sort.php

i.e. sort(myArray, SORT_LOCALE_STRING);

Hope that helps,
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:  http://us4.php.net/manual/en/function.html-entity-decode.php

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now