Sorting my javascript dropdown by name not key #

I'm trying to sort my drop-down menu (hash) in alphabetical order ... I tried the sort method that was posted, but all I get is "undefined" for each dropdown menu name

here's the hash table:

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 it drives me crazy!

no sorting function: the drop-down menu captures the lowest key #, which will be 3, and then displays โ€œBELLโ€ in the list that I really want the โ€œAlterna Savinsโ€ to appear at the top of the drop-down list.

+1
source share
2 answers

In the sort function, the key array should receive the object that the key points to, not the key. You can also store the key value in each object of the array, to refer to it later. Then, the sort function must compare 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: '...'}, {}, {}] 
+3
source

If you change the structure of clientProjectsHash , sorting becomes easier. But I'm not sure how you use clientProjectsHash . So my suggestion could be a performance issue in incompatible 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 saved the key values โ€‹โ€‹as an object key for quick access. But now the loops are faster. So, you can try such decisions and decide where to go, instead of going hard and suffering.

Good luck.

+1
source

Source: https://habr.com/ru/post/1490327/


All Articles