1

i'm trying to sort my pulldown menu (hash) in alphabetical order... i tried a sort method someone posted but all i get is 'undefined' for each pulldown menu name

here's the hashtable:

        var clientProjectsHash = {};
    clientProjectsHash['4'] = {};
    clientProjectsHash['4']['name'] = 'Alterna Savins & Credit Union';
    clientProjectsHash['4']['projects'] = {};
    clientProjectsHash['5'] = {};
    clientProjectsHash['5']['name'] = 'BDC';
    clientProjectsHash['5']['projects'] = {};
    clientProjectsHash['3'] = {};
    clientProjectsHash['3']['name'] = 'BELL';
    clientProjectsHash['3']['projects'] = {};
    clientProjectsHash['6'] = {};
    clientProjectsHash['6']['name'] = 'BNC';
    clientProjectsHash['6']['projects'] = {};

function getSortedKeys(obj) {
    var keys = []; for(var key in obj) keys.push(key);
    return keys.sort(function(a,b){return obj[a]-obj[b]});
}

function populateClientSelect(selectedClientId) {
        //get the client select
        var clientSelect = document.getElementById('clientSelect');

        clientProjectsHash = getSortedKeys(clientProjectsHash);


        //add the clients
        for (clientKey in clientProjectsHash) {

            clientSelect.options[clientSelect.options.length] = new Option(clientProjectsHash[clientKey].name, clientKey);

            if(selectedClientId == undefined || selectedClientId == 0) {
                if(clientKey > 0) {
                    selectedClientId=clientKey;
                }
            }

            if (clientKey == selectedClientId)
                clientSelect.options[clientSelect.options.length-1].selected = true;
        }
    }

everything i try does NOT work and this is driving me nuts!

without the sort function: the pulldown menu grabs the lowest key # which would be 3 and then outputs 'BELL' on the list which i actually want "Alterna Savins" to show up at the top of the pull down list..

2 Answers 2

3

In the sort function, the keys array should be receiving the object pointed by the key and not the key instead. You could also store the value of the key in each array object to reference it later. Then the sort function should be comparing the name property of each object:

function getSortedKeys(obj) {
    var keys = []; 
    for(var key in obj) { 
        keys.push(obj[key]);
        keys[keys.length-1]['key'] = key;
    }
    return keys.sort(function(a,b){ return a.name > b.name ? 1 : a.name < b.name ? -1 : 0;});
} // returns [{name:'...', projects:'...', key: '...'}, {}, {}]
Sign up to request clarification or add additional context in comments.

2 Comments

this still doesn't work... its grabbing BELL [3] and posting it to the top of the list... i wish there was a solution for it to post Alterna Savins [4] first...
getSortedKeys returns an array; iterate over it as such: clientProjectsHash = getSortedKeys(clientProjectsHash); for (var i = 0; i < clientProjectsHash.length; i++) { alert(clientProjectsHash[i].key); alert(clientProjectsHash[i].name); }
1

If you change clientProjectsHash structure, sorting would become easier. But, I am not sure how you are using clientProjectsHash. So, my suggesstion could be a performance issue in non modern browsers.

var clientProjectsHash = [];
clientProjectsHash[0] = {key: 4, name: 'Alterna Savins & Credit Union', projects: {}};
clientProjectsHash[1] = {key: 5, name: 'BDC', projects: {}};
clientProjectsHash[2] = {key: 3, name: 'BELL', projects: {}};
clientProjectsHash[3] = {key: 6, name: 'BNC', projects: {}};

function sorter(a, b) {
  if( a.name < b.name ) return -1;
  if( a.name > b.name ) return 1;
  return 0;
}

clientProjectsHash.sort(sorter);

I assume that you kept the key values as object key for fast access. But, for loops are faster now. So, you could try solutions like this & decide which way to go, instead of taking a hard way & suffer.

best of luck.

1 Comment

i also tried this and it works aswell.. thanks for this other option! appreciate it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.