Apologies if this should be a Views issue rather than a Location issue. I've got a view containing a list of locations sorted by country, then state, city, etc. The sort order is being determined by the country code rather than the name, even though the name is displayed correctly in the view. This is confusing for users, as the resulting list has United Arab Emirates (ae) appearing first, followed by Australia (au).
Thanks,
Daniel

Comments

yesct’s picture

Status: Active » Postponed (maintainer needs more info)

can you change the sort order from country code to name? Also, try this again in the most recent dev version.

drupalok’s picture

same here. how can i change the sort order to name? please help! :)
i am already using lates DEV version

thanks!!!

swolynski’s picture

Any resolution to this? I'm encountering the same problem. Thanks.

drupalok’s picture

@ YesCT
What information do you need?
Thanks for this cool module

yesct’s picture

Priority: Normal » Minor
Status: Postponed (maintainer needs more info) » Active
Issue tags: +location country name vs country code

hmm. I see what you mean.
http://cathytheys.blip.tv/file/3439508

I guess we need someone to look at the code and figure out how to provide a way to sort by the name, not the code. I dont know if asking a views person to lend a hand would be helpful?

Maybe try going in to #drupal or #drupal-support and saying
"hi. I want to make by first patch. where do I look in the code to find out how a views sort order is set?" ...

making a patch
http://drupal.org/patch/create
irc
http://drupal.org/irc

usually people in irc like to help new people become the kind of people who can patch stuff. :)

philipz’s picture

This is not Views problem but the way country names are stored in associative array in location.inc file.
If they were stored in database this would be possible to sort in views query but for now the only way is to write a module like this:

location_country_sorting.info

name = Location Country Sorting
description = "Fix views sorting of location nodes by country name."
core = 6.x
package = Location

dependencies[]  = views 
dependencies[]  = location 

location_country_sorting.module

function location_country_sorting_views_pre_render (&$view) {
  if (isset($view->sort['country'])) {
    $countries = array();
    $result = array();
    for($i = 0; $i<count($view->result); $i++) {
      $countries[location_country_name($view->result[$i]->location_country)] = $i;
    }
    ksort($countries);
    foreach($countries as $weight) {
      $result[] = $view->result[$weight];
    }
    $view->result = $result;
  }
}
tomdisher’s picture

This sorts the countries correctly but when using views according it will only show one result in the accordian per country, even if there are many results that should be found.

chrisbudy’s picture

If you were to modify the key for the associative array that's being setup to be less generic you should get all results for each listed country.

$countries[location_country_name($view->result[$i]->location_country) . "-" . $i ] = $i;

We had a similar issue with sorting by province, and philipz solution here pointed us in the right direction.

http://www.heavyrobot.com/blog/provincestate-sorting-issues-views-and-lo...

mattcasey’s picture

I used #6 and #8 with a slight modification, maybe because we are using Content Profile: the result was in "node_users__location_country"

So, in the module file:

function location_country_sort_views_pre_render (&$view) {
  if (isset($view->sort['country'])) {
    $countries = array();
    $result = array();
    for($i = 0; $i<count($view->result); $i++) {
      $countries[location_country_name($view->result[$i]->node_users__location_country) . "-" . $i ] = $i;
    }
    ksort($countries);
    foreach($countries as $weight) {
      $result[] = $view->result[$weight];
    }
    $view->result = array_reverse($result);
  }
}
m2calabr’s picture

Status: Active » Needs review
StatusFileSize
new6.28 KB

I have created a patch to 6.x-3.x and allows for countries to be truly sorted by country name in views properly. I had to create a new location_country table that I could join to in the SQL, so the SQL order by works correctly. The table will be create on a new install and if you run update.php. This patch also fixes a small issues where provinces in Japan that don't have abbreviations just numbers. These are not proper for mailing labels where you must the abbreviation, but must have the full name in those instances. So, if the province is just a number it is replaced with the full province name.

podarok’s picture

Version: 6.x-3.0 » 6.x-3.x-dev
Status: Needs review » Active
Issue tags: -location country name vs country code

bot recall

podarok’s picture

Status: Active » Needs review

bot

podarok’s picture

Version: 6.x-3.x-dev » 7.x-3.x-dev
Category: bug » task
Priority: Minor » Critical
Status: Needs review » Patch (to be ported)

#10 pushed to 6.x-3.x-dev
Thanks!!

This one has to be ported to 7.x-3.x-dev

podarok’s picture

Status: Patch (to be ported) » Needs review

Status: Needs review » Needs work

The last submitted patch, locations-country-sort-380842-10.patch, failed testing.

jerenus’s picture

Status: Needs work » Needs review
StatusFileSize
new6.62 KB

Rerolled to 7.x-3.x-dev.

podarok’s picture

Status: Needs review » Needs work
jerenus’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work
jerenus’s picture

Status: Needs work » Needs review
StatusFileSize
new6.85 KB

Retesting.

jerenus’s picture

podarok’s picture

Priority: Critical » Normal
Status: Needs review » Needs work

#22 commited pushed to 7.x-3.x-dev
Thanks!
do git pull for next task
-----------
There are a few nitpicks for release tagging
All debug code should be removed

+    function extra_options_form(&$form, &$form_state) {
+        //drupal_set_message('location_handler_sort_location_country inside extra_options_form');
+        $form['country_sort'] = array(
+    function query() {
+        //drupal_set_message('location_handler_sort_location_country inside query');
+
+        //drupal_set_message('Options:' . print_r($this->options,1));
+        if ($this->options['country_sort']=='name') {
+            //drupal_set_message('Select sort by name');
+        }  elseif ($this->options['country_sort']=='code') {
+            //drupal_set_message('Select sort by code');

looks like missed info strings...

+        //Create the join definition with the location_county table that has the mapping from
+        //country code to country name
+    }
+}
diff --git a/location.install b/location.install
jerenus’s picture

Status: Needs work » Needs review
StatusFileSize
new2.23 KB

Removed~ ^.^

podarok’s picture

Status: Needs review » Fixed

#24 commited

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

podarok’s picture

Priority: Normal » Critical
Status: Closed (fixed) » Active
podarok’s picture

Status: Active » Fixed

fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.