BinarySearch::usage = "BinarySearch[list, key, getkey] finds the position
	in list of a record whose key value is equal to key. The function getkey
	is applied to records to retrieve the key for comparisons."

BinarySearch[list_, key_, getkey_] :=
	Module[{n0 = 1, n1 = Length[list], m},
		While[n0 <= n1,
			m = Floor[(n0 + n1)/2];
			Switch[ Order[ getkey[list[[m]]], key ],
				1,  n0 = m + 1,           (* record is above m *)
				0,  Return[ list[[m]] ],  (* found! *)
				-1, n1 = m - 1            (* record is below m *)
			]
		];
		Null	(* record is not in list *)
	]
