BUG?! *** Array.BinarySearch sucks!

M

Mario

Hi there,

I'm writing a piece of code with VS.Net 2003, Framework 1.1.
And I can't make BinarySearch to work right. Look at this sample:


Dim sexy() As String = {"-", "a", "b", "ba", "A", "Aa", "Ab", _
"Aba", "B", "Ba", "Bb", "Bba", "BA", "BAa", "BAb", "BAba"}
Dim index As Integer

index = Array.BinarySearch(sexy, "BAba") ' index = -11 ?? BUG!!
Debug.WriteLine(index.ToString) ' How no match???

index = Array.BinarySearch(sexy, "a") ' index = 1
Debug.WriteLine(index.ToString) ' this one works fine

' so, wtf I'm doing wrong?


Please help me if you can, because I'm bugged; and I dont want to go
with a silly For Each, If Then, snoopy loopy. Yea, I know I can also use
the Array.IndexOf, but expert writter Francesco Balena says it's slower
then BinarySearch on large arrays, that is my case.

Thank you very much!
Kind regards, :)
Mario
 
O

One Handed Man

Simple, your array is not sorted. BAb is smaller than b because the sort order is character by character and B is smaller than b so should appear before it.

Regards - OHM

--
Best Regards - OHM
one.handed.man{at}BTInternet{dot}com

Hi there,

I'm writing a piece of code with VS.Net 2003, Framework 1.1.
And I can't make BinarySearch to work right. Look at this sample:


Dim sexy() As String = {"-", "a", "b", "ba", "A", "Aa", "Ab", _
"Aba", "B", "Ba", "Bb", "Bba", "BA", "BAa", "BAb", "BAba"}
Dim index As Integer

index = Array.BinarySearch(sexy, "BAba") ' index = -11 ?? BUG!!
Debug.WriteLine(index.ToString) ' How no match???

index = Array.BinarySearch(sexy, "a") ' index = 1
Debug.WriteLine(index.ToString) ' this one works fine

' so, wtf I'm doing wrong?


Please help me if you can, because I'm bugged; and I dont want to go
with a silly For Each, If Then, snoopy loopy. Yea, I know I can also use
the Array.IndexOf, but expert writter Francesco Balena says it's slower
then BinarySearch on large arrays, that is my case.

Thank you very much!
Kind regards, :)
Mario
 
O

One Handed Man

In fact to further illustrate this, if you use the sort method, look at your array after it is sorted, it sorts the array in descending order.

--
Best Regards - OHM
one.handed.man{at}BTInternet{dot}com

Simple, your array is not sorted. BAb is smaller than b because the sort order is character by character and B is smaller than b so should appear before it.

Regards - OHM

--
Best Regards - OHM
one.handed.man{at}BTInternet{dot}com

Hi there,

I'm writing a piece of code with VS.Net 2003, Framework 1.1.
And I can't make BinarySearch to work right. Look at this sample:


Dim sexy() As String = {"-", "a", "b", "ba", "A", "Aa", "Ab", _
"Aba", "B", "Ba", "Bb", "Bba", "BA", "BAa", "BAb", "BAba"}
Dim index As Integer

index = Array.BinarySearch(sexy, "BAba") ' index = -11 ?? BUG!!
Debug.WriteLine(index.ToString) ' How no match???

index = Array.BinarySearch(sexy, "a") ' index = 1
Debug.WriteLine(index.ToString) ' this one works fine

' so, wtf I'm doing wrong?


Please help me if you can, because I'm bugged; and I dont want to go
with a silly For Each, If Then, snoopy loopy. Yea, I know I can also use
the Array.IndexOf, but expert writter Francesco Balena says it's slower
then BinarySearch on large arrays, that is my case.

Thank you very much!
Kind regards, :)
Mario
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top