Groups | Blog | Home
all groups > dotnet framework > may 2006 >

dotnet framework : Array.BinarySearch ALWAYS NEGETIVE


JP
5/5/2006 1:51:02 PM
Why doesn’t this work? If State="NY", the resulting IF statement returns 31,
if the State="NC" the resulting IF returns -27, yet NC is in the array list.
Its random as to which stats work and which do not. AL will return 0, but AR
will return -3, yet its in the list. I know the value when neg (-) is a
bitwise representation, but most every state in the array list returns a neg
number, Why?

string[] strStateArray = new string[]
{"AL","AK","AZ","AR","CA","CO","CT","DE","FL","GA","HI","ID","IL","IN","IN","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","DC","WV","WI","WY"};


if(Array.BinarySearch(strStateArray, State.ToString())<0)
{
ValidationStatus="Incoming State is invalid. The follow state
abrevations are acceptable: " + strStateArray.ToString();
}

--
JP
JP
5/5/2006 2:35:01 PM
Oh duh! Thank you so much. Its been one of those Fridays.
--
JP
..NET Software Develper


[quoted text, click to view]
pvdg42
5/5/2006 4:08:45 PM

[quoted text, click to view]

The elements to be searched by a binary search algorithm must be sorted.
Your failures (AZ, NC, etc) are out of order, and therefore, not found.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.

Ben Voigt
5/12/2006 11:50:54 AM

[quoted text, click to view]

More precisely, it looks like the state names, not the abbreviations, were
sorted
"New York" < "North Carolina" but "NC" < "NY"

[quoted text, click to view]

AddThis Social Bookmark Button