For example, I am making a select box that lists users alphabetically. Users are identified in the database by an id:
var foo = new Element('select').inject($$('body')[0]);
var options = {5082:'User A', 5085:'User B', 5074:'User C'}
$H(options).each(function(name,id){
new Element('option', {text:name, value:id}).inject(foo);
});
/*
Function: Hash.sort
Takes {c:0, a:1, b:2} and returns [{a:1},{b:2},{c:0}]
*/
Hash.Implement({
sort:function(fn){
var out = [],
keysToSort = this.getKeys(),
m = this.getLength();
(typeof fn == 'function') ? keysToSort.sort(fn) : keysToSort.sort();
for (var i=0; i<m; i++){
var o = {};
o[keysToSort[i]] = this[keysToSort[i]];
out.push(o);
}
return out;
}
});
____________ });
Usage:
$H({'a':1,'z':2,'c':3,'b':4}).sort()
Results In:
[Object { a=1}, Object { b=4}, Object { c=3}, Object { z=2}]
______________________
Then our select box routine becomes:
var foo = new Element('select').inject($$('body')[0]);
var options = {5082:'User A', 5085:'User B', 5074:'User C'}
$H(options).sort().each(function(el){
$each(el,function(name, id){
new Element('option', {text:name, value:id}).inject(foo);
});
});
_______ Ruby programmers may recognize this method (http://ruby-doc.org/core/
classes/Hash.html#M002865).