我正在尝试按字母顺序对我的哈希表下拉菜单进行排序...使用此功能:
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;
});
}
这对下拉菜单进行排序......虽然它改变了我的菜单项的原始 id#,这在我的网站上搞砸了一些事情......是否可以保留每个菜单项的原始 id# 并仍然排序?
抱歉..这是哈希码:
var clientProjectsHash = {};
clientProjectsHash['1'] = {};
clientProjectsHash['1']['name'] = 'RONA';
clientProjectsHash['2'] = {};
clientProjectsHash['2']['name'] = 'CMS';
clientProjectsHash['3'] = {};
clientProjectsHash['3']['name'] = 'ALT';
并且 getSortedKeys 由以下方式调用:
function getInitialClient() {
clientProjectsHash = getSortedKeys(clientProjectsHash);
for (clientKey in clientProjectsHash) {
if(clientKey > 0) {
return clientKey;
}
}
}