Re: find closest item in keyed collection
- From: christery@xxxxxxxxx
- Date: Wed, 19 Dec 2007 09:04:22 -0800 (PST)
Ben, Tested that on the original q, as I understand it, the "close
find" shuld give 40 if asked 39...
Array i2 = Array.CreateInstance(typeof(short),7);
// given 20, 30, 40, 50, 60, 70, 80
i2.SetValue((short)20, 0);
i2.SetValue((short)30, 1);
i2.SetValue((short)40, 2);
i2.SetValue((short)50, 3);
i2.SetValue((short)60, 4);
i2.SetValue((short)70, 5);
i2.SetValue((short)80, 6);
// given 35
int j = Array.BinarySearch(i2, (short)35);
if (j < -1)
j = ~j-1;
Console.WriteLine("35 -> " +
i2.GetValue(j).ToString()); // 30
// testing low 10
j = Array.BinarySearch(i2, (short)10);
if (j < -1)
j = ~j - 1;
else // Oops
j = 0;
Console.WriteLine("10 -> " +
i2.GetValue(j).ToString()); // 20
// testing high 100
j = Array.BinarySearch(i2, (short)100);
if (j < -1)
j = ~j - 1;
Console.WriteLine("100 -> " +
i2.GetValue(j).ToString()); // 80
// testing close 39
j = Array.BinarySearch(i2, (short)39);
if (j < -1)
j = ~j - 1;
Console.WriteLine("39 -> " +
i2.GetValue(j).ToString()); // 30 = Fail, should return 40 or what?
//CY
.
- Follow-Ups:
- References:
- find closest item in keyed collection
- From: Steve Richter
- Re: find closest item in keyed collection
- From: Ben Voigt [C++ MVP]
- Re: find closest item in keyed collection
- From: Liz
- Re: find closest item in keyed collection
- From: Steve Richter
- Re: find closest item in keyed collection
- From: Marc Gravell
- Re: find closest item in keyed collection
- From: Ben Voigt [C++ MVP]
- Re: find closest item in keyed collection
- From: Liz
- find closest item in keyed collection
- Prev by Date: Re: MMC 3.0 and .NET 1.1 ???
- Next by Date: Hyperlink click event trigger using Aspxdatagridview control
- Previous by thread: Re: find closest item in keyed collection
- Next by thread: Re: find closest item in keyed collection
- Index(es):
Relevant Pages
|