Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - how to do Generic.List.BinarySearch on byRef data?

Reply
 
Old 06-18-2008   #1 (permalink)
Buky


 
 

how to do Generic.List.BinarySearch on byRef data?

So, Let's say that I have an class with properies like Name, LastName & ID.
I put them into List(of myClass) and I would like to search through that
List based on my search criteria (Name & Id for example).
So, I create an Comparer, and provide it's 'reference' to an BinarySearch
functio, but each time I got incorrect results.
Where could be problem in following code?

Public Class myClass
Public Id As Int32
Public Name As String
Public LastName As String

Public Sub New()
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class

Public Sub test_novi()
Dim Lista As New List(Of myClass)
Dim y As Int32
Dim C As IComparer(Of MyClass)
C = New MyCompare
Dim T As New MyClass

T.Id = 1
T.Name = 2
Lista.Add(T)
T = New MyClass
T.Id = 2
T.Name = 4
Lista.Add(T)
T = New MyClass
T.Id = 3
T.Name = 6
Lista.Add(T)

Lista.Sort()
Dim T1 As New MyClass
T1.Id = 1
T1.Name = 2

y = Lista.BinarySearch(T1, C)
End Sub
End Module

Public Class MyCompare
Implements IComparer(Of MyClass)
Public Sub New()
End Sub

Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
Public Function Compare(ByVal x As MyClass,byVal y As MyClass) As
Integer Implements IComparer(Of MyClass).Compare
If x.Id = y.Id Then
If x.Name = y.Name Then
Return 0
Else
Return -1
End If
Else
Return -2
End If
End Function
End Class




My System SpecsSystem Spec
Old 06-18-2008   #2 (permalink)
Jon Skeet [C# MVP]


 
 

Re: how to do Generic.List.BinarySearch on byRef data?

Buky <necunecu@xxxxxx> wrote:
Quote:

> So, Let's say that I have an class with properies like Name, LastName & ID.
> I put them into List(of myClass) and I would like to search through that
> List based on my search criteria (Name & Id for example).
> So, I create an Comparer, and provide it's 'reference' to an BinarySearch
> functio, but each time I got incorrect results.
> Where could be problem in following code?
Well:

1) BinarySearch only works if the list is already ordered to start
with, in an order which is consistent with the comparer you use.
2) Your comparer is inconsistent - in particular, if you do
Compare(x, y) and Compare(y, x) it could return a negative number
both times - meaning that x < y and y < x.
3) You appear to have finalizers for no reason, which is a generally
bad thing to do. (You also have public fields, but I'll assume
you wouldn't do that normally.)

If you make your comparer more sensible, and sort *passing in the
comparer* before calling BinarySearch it should work.

--
Jon Skeet - <skeet@xxxxxx>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
My System SpecsSystem Spec
Old 06-18-2008   #3 (permalink)
Buky


 
 

Re: how to do Generic.List.BinarySearch on byRef data?


"Jon Skeet [C# MVP]" <skeet@xxxxxx> wrote in message
news:MPG.22c3856939184fb0d94@xxxxxx
Quote:

> Buky <necunecu@xxxxxx> wrote:
Quote:

>> So, Let's say that I have an class with properies like Name, LastName &
>> ID.
>> I put them into List(of myClass) and I would like to search through that
>> List based on my search criteria (Name & Id for example).
>> So, I create an Comparer, and provide it's 'reference' to an BinarySearch
>> functio, but each time I got incorrect results.
>> Where could be problem in following code?
>
> Well:
>
> 1) BinarySearch only works if the list is already ordered to start
> with, in an order which is consistent with the comparer you use.
does it means that I need to make some 'ordering' before binarySearch?
Quote:

> 2) Your comparer is inconsistent - in particular, if you do
> Compare(x, y) and Compare(y, x) it could return a negative number
> both times - meaning that x < y and y < x.
At this moment, it's not so important for me... I would like just to make it
work
Quote:

> 3) You appear to have finalizers for no reason, which is a generally
> bad thing to do. (You also have public fields, but I'll assume
> you wouldn't do that normally.)
yea, this is just an example...
Quote:

> If you make your comparer more sensible, and sort *passing in the
> comparer* before calling BinarySearch it should work.
I tried to call Sort sub before it, but it gave me an exception
(InvalidOperationException - failed to compare two elements in the array)
Quote:

> --
> Jon Skeet - <skeet@xxxxxx>
> Web site: http://www.pobox.com/~skeet
> Blog: http://www.msmvps.com/jon.skeet
> C# in Depth: http://csharpindepth.com

My System SpecsSystem Spec
Old 06-18-2008   #4 (permalink)
Jon Skeet [C# MVP]


 
 

Re: how to do Generic.List.BinarySearch on byRef data?

Buky <necunecu@xxxxxx> wrote:
Quote:
Quote:

> > Well:
> >
> > 1) BinarySearch only works if the list is already ordered to start
> > with, in an order which is consistent with the comparer you use.
>
> does it means that I need to make some 'ordering' before binarySearch?
You can call Sort and pass in an IComparer<T>, so that things are
sorted in a consistent manner first.
Quote:
Quote:

> > 2) Your comparer is inconsistent - in particular, if you do
> > Compare(x, y) and Compare(y, x) it could return a negative number
> > both times - meaning that x < y and y < x.
>
> At this moment, it's not so important for me... I would like just to make it
> work
Well it's important to the framework!

Think about it - binary searches only work if the items are in a
consistent order to start with. It's impossible for the items to *be*
in a consistent order when one item is simulataneously greater than and
less than another.
Quote:
Quote:

> > If you make your comparer more sensible, and sort *passing in the
> > comparer* before calling BinarySearch it should work.
>
> I tried to call Sort sub before it, but it gave me an exception
> (InvalidOperationException - failed to compare two elements in the array)
You need to pass in the IComparer<T> you're going to use for the binary
search, but it will need to be consistent.

--
Jon Skeet - <skeet@xxxxxx>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
V2 CTP3: How to use a generic List constructor PowerShell
Generic Class: Passing Data Type to Placeholder .NET General
Initializing a generic SortedList with sorted data .NET General
System.Collections.Generic.List<int> myList = new System.Collections.Generic.List<int>(100); .NET General
Get-Member -InputObject returns generic data; bug? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46