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..