I'm having an issue with finding good approach to make sorting works in a Drupal Views.

I've multilingual website (1 database for all languages). Few languages are sorted wrong.

I've read that the real issue is on database layer, however I can't set any specific encoding there, since database contains many different languages.

What would be the most correct approach to make sorting works? The minimum I need atm is a sorting in a Drupal Views.

Any good approach of solving such issues?

| improve this question | | | | |
  • I think that until you've sorted out things on the db layer, this question is going to be off-topic. Check what encoding you currently have on your db, and do some research on that topic. Then you can move the focus to "How can I get Views to handle X as Y", which would be on-topic. – Letharion Feb 25 '15 at 10:48
  • @letharion I agree, that 'right' solution must be done on db layer, however I think it is to late (I would be happy to be wrong), since we have only 1 database for all languages for almost 1 year. What would you recommend me to do in such case? – Dmytro Pastovenskyi Feb 25 '15 at 12:28

It may help to somebody.

I end up with doing a simple module (which I'm gonna improve later) that inject COLLATE while doing query to Database so it sort everything properly.

Please feel free to comment this solution :)


 * @file
 * Implements hook_views_query_alter().
 * it adds 'COLLATE utf8_language_ci' to languages that are not supported by default
 * so far there are only few languages we have issues with.
function views_collate_sorting_views_query_alter(&$view, &$query) {
  // views where we want to enable collation
  $views = array(

  // exit if the current view is not specified
  if (!in_array ($view->name, $views)) {
    return FALSE;

  // define list of languages we support
  $map = array(
    'da' => 'utf8_danish_ci',
    'sv' => 'utf8_swedish_ci',
    'nb' => 'utf8_danish_ci',
    'es' => 'utf8_spanish_ci',
    'pl' => 'utf8_polish_ci',

  // get current utf8
  $l = $GLOBALS['language']->language;

  if (!array_key_exists ($l, $map)) {
    return FALSE;

  // get current utf8 language which we are going to add to all sort fields
  $utf8 = $map[$l];

  // add COLLATE utf8_xx_ci to all sort criteria
  $orderby = &$query->orderby;
  foreach ($orderby as &$order) {
    $order['field'] .= ' COLLATE ' . $utf8;
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.