RC <ra**********@noaa.gov> writes:
I have simple hashtable object
function myHashTable(key, value) {
this.key = key;
this.value = value;
}
This is really a hash table entry, not a full hash table?
var myObjectArray = new Array(5);
myObjectArray[0] = new myHashTable(1, "2006-06-07");
myObjectArray[1] = new myHashTable(2, "2005-04-10");
....
Now my question is how do I sort the value, not the key
in myObjectArray?
myObjectArray.sort(functionThatComparesMyHashTable ByValue);
The hard part is making that function :)
A generic comparison function, for anything that works with the less
than operator, is:
function compare(a,b) {
return (a < b) ? -1 : (b < a) ? 1 : 0;
}
You can then build a comparison function for the values:
function functionThatComparesMyHashTableByValue(a,b) {
return compare(a.value, b.value);
}
A shorter name would be recommended :)
I did
myObjectArray.sort(myHashTable);
The argument to the sort function must be a comparison function
returning a number, the sign of that number representing whether
one argument is smaller or bigger than the other.
You are passing a function that takes two arguments and assigns them
to some object (in this case the global object, as you can test by
alerting window.key and window.value afterwards :).
But the output result are NOT correct.
Unsurprisingly! (or really, with only two elements in the array,
there's a 50% chance the sort would be correct if it's random :)
Hope this helps. Code not tested.
/L
--
Lasse Reichstein Nielsen -
lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'