Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NumericComparer ?? 2

Status
Not open for further replies.

Sniipe

Programmer
Oct 9, 2006
115
IE
I have a compare function as follows:
Code:
Private Class QuoteSorter
        Implements IComparer

        ' Calls CaseInsensitiveComparer.Compare with the parameters reversed.
        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
            Dim q1 = x.quoteid
            Dim q2 = y.quoteid
            Return New CaseInsensitiveComparer().Compare(q1, q2)
        End Function
    End Class
It works perfectly for quoteID's such as ABC123, ABC234, ABC222
But now I want to compare doubles (where as before I was comparing strings)

The call to the above function would be
Code:
' sort the array by quoteid
        Me.Sort(New QuoteSorter)
 
There is a forum for VB.net forum796

But shouldn't you be able to do this with some simple math?

It is curious that it doesn't seem to work with Doubles though...

Hope this helps,

Alex

[small]----signature below----[/small]
I'm pushing an elephant up the stairs

My Crummy Web Page
 
oops sorry - I'm not used to vb.net - I just went to my quick link for c# thanks

I did get it working

Code:
Private Class PromotionSorterByDiscountValue
        Implements IComparer

        ' Calls CaseInsensitiveComparer.Compare with the parameters reversed.
        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
            Dim q1 As Double
            Dim q2 As Double
            Dim result As Double

            q1 = x.MarketingDiscount
            q2 = y.MarketingDiscount
            
            result = q2 - q1
            If result > 0 Then
                Return 1
            ElseIf result < 0 Then
                Return -1
            Else
                Return 0
            End If
        End Function
    End Class
and the call
Code:
Me.Sort(New PromotionSorterByDiscountValue)

Works fine for me and it sorts decending
 
and the short veriosn is.

Code:
Private Class PromotionSorterByDiscountValue
        Implements IComparer

        ' Calls CaseInsensitiveComparer.Compare with the parameters reversed.
        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
                        x.MarketingDiscount.compareto(y.MarketingDiscount)
        End Function
    End Class




Christiaan Baes
Belgium

My Blog
 
vb isn't the nicest thing to move to from c#

Thanks chrissie1 - I may fix that later on (hate fixing stuff when its not broke)

Can't believe I'm working with vb6 and vb.net 1.1 - I've fallen a long way from c#.net 2.0
 
Hey, sometimes you've got no choice in using VB.Net -- the boss is the one writing the paychecks, you know.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Hey, sometimes you've got no choice in using VB.Net -- the boss is the one writing the paychecks, you know.

Like VB.Net is that bad.
Really chip, you disappoint me ;-).

Christiaan Baes
Belgium

My Blog
 
I can say this, because I wrote VB6 for money, but used C# at home for personal projects.
:)

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top