private function sortQuery(SelectInterface $query, $name, $suggestion_format) {
    if ($suggestion_format !== SOCIAL_PROFILE_SUGGESTIONS_USERNAME && $this->useFullName()) {
      // Delete percent symbol on the beginning of the phrase for search from
      // the start of field values.
      $name = substr($name, 1);

      $query->addExpression("
    CASE WHEN fn.field_profile_first_name_value LIKE '$name' THEN 0
      WHEN ln.field_profile_last_name_value LIKE '$name' THEN 1
      ELSE 2
    END
  ", 'mention_sort');
      $query->orderBy('mention_sort');
      $query->orderBy('fn.field_profile_first_name_value');
      $query->orderBy('ln.field_profile_last_name_value');
    }

    if ($this->addNickName() === TRUE) {
      $query->orderBy('nn.field_profile_nick_name_value');
    }

    $query->orderBy('uf.name');

    return $query;
  }