By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,168 Members | 1,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,168 IT Pros & Developers. It's quick & easy.

BUG?! *** Array.BinarySearch sucks!

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

"Mario" <mz******@DONTWANTSPAMmail.pt> wrote in message news:uz**************@tk2msftngp13.phx.gbl...
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
Nov 20 '05 #2

P: n/a
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

"One Handed Man" <he******@hotmail.com> wrote in message news:c0**********@hercules.btinternet.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

"Mario" <mz******@DONTWANTSPAMmail.pt> wrote in message news:uz**************@tk2msftngp13.phx.gbl...
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
Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.