2

I have an issue in sorting alphabetically those items in the state/region drop down.

// File: Mage/Directory/Helper/Data.php (line:150)
$json = Mage::helper('core')->jsonEncode($regions); 
Mage::log($json);

I debugged above line and had checked the order in this var $json as expected. However, when json data comes to client-side, somehow that json object sorted itself based on the region_id. Is there a way to stop this auto-sort?

2 Answers 2

4

I think the problem in your case is not the code. It's the browser.
You can read more about it here.

And here is a simple test you can run in the console.

var a = {1:'a', 3:'b', 2: 'c'};
console.log(a);

the result will be (at least on chrome and FF):

Object { 1="a", 2="c", 3="b"}  

I was able to overcome this with the following trick.

In js/varien/form.js in the class RegionUpdater I added an other function to sort the region dropdown.

sortSelect : function (){
    elem = this.regionSelectEl;
    var tmpAry = new Array();
    var currentVal = $(elem).value;
    for (var i=0;i<$(elem).options.length;i++) {
        if (i == 0) continue;
        tmpAry[i-1] = new Array();
        tmpAry[i-1][0] = $(elem).options[i].text;
        tmpAry[i-1][1] = $(elem).options[i].value;
    }
    tmpAry.sort();
    while ($(elem).options.length > 0) {
       $(elem).options[0] = null;
    }
    for (var i=1;i<=tmpAry.length;i++) {
        var op = new Option(tmpAry[i-1][0], tmpAry[i-1][1]);
        $(elem).options[i] = op;
    }
    $(elem).value = currentVal;
    return;
}

and just call this function inside the update function of the same class.

Right before if (this.disableAction=='hide') { add this line

this.sortSelect();
2
  • appreciate for the effort you put in. Your code is worked. And, I hope one day this auto-sort on json object would be removed from all browsers and leave it to the programmer choice. *crossed fingers
    – Leongelis
    Apr 8, 2014 at 1:54
  • Can you please guide me for Magento 2 file location ? Oct 21, 2020 at 7:24
3

I'm not sure what you are exactly talking about. Where in magento, registration or checkout? But I'll try.

The regions are ordered by name by default:

\Mage_Directory_Model_Resource_Region_Collection::_construct
protected function _construct()
{
    $this->_init('directory/region');

    $this->_countryTable    = $this->getTable('directory/country');
    $this->_regionNameTable = $this->getTable('directory/country_region_name');

    $this->addOrder('name', Varien_Data_Collection::SORT_ORDER_ASC);
    $this->addOrder('default_name', Varien_Data_Collection::SORT_ORDER_ASC);
}

So when your regions are not ordered, something is wrong.

You should check where the ordering is changed.

2
  • Apologize for unclear wording. The region I mentioned is at customer address form whereby customer can change the address detail. app\design\frontend\base\default\template\customer\address\edit.phtml and line 163
    – Leongelis
    Apr 8, 2014 at 1:03
  • Plus for the example on how regions is actually sorted in the constructor.
    – electroid
    Dec 12, 2016 at 6:53

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.