If you're developing a Firefox extension, and you use Javascript's Array.sort to sort an associative array, you might not like the results. As far as I can tell, it does nothing. I built an array keyed on a field called loc, which wasn't to be used in sorting, and another field seq, which was to be used. Multiple rows could have the same loc, so the associative array was handy in allowing me to easily make sure only the latest seq for each loc was recorded. Alas, it was not to be.
However, you can get around this by giving your array numeric indexes. I needed to ensure that each loc only used one index and if it was already in sorted, I should overwrite that array element. Here's the bit of code I used to make a sortable array:
var sorted = new Array();
var locKeys = new Array();
for( var r in rows )
{
var ad = rows[r];
instance.seq = getSeq(ad);
instance.loc = getLoc(ad);
if( !(instance.loc in locKeys) )
{
locKeys[instance.loc] = sorted.length;
}
sorted[locKeys[instance.loc]] = instance;
}
Now sorted will actually re-arrange itself when I call sorted.sort().
About me: I'm doing my best to be peaceful, non-violent, and humble as I seek epiphanies and try to help others find them too. I identify with my kids and everyone that my life will affect into the future, so I take a long term view of things. Religion and taxes are avoidable evils. Spirituality, freedom, individual sovereignty, and voluntary cooperation will eventually replace them - maybe in my lifetime if you help.
Sunday, August 30, 2009
Friday, August 28, 2009
Modern Borrowing
If you let someone borrow something, you must accept the risk that they will not willingly return to you as much value as they borrowed. Modern society allows this mistake to run rampant by providing the illusion that you can force someone to return that value. It's an illusion because the cost of forcing them to return the value quite high. The illusion and it's ugly consequences persist because these costs are spread so thinly across everyone.
Subscribe to:
Posts (Atom)