3 Replies - 798 Views - Last Post: 09 November 2012 - 06:27 PM Rate Topic: -----

#1 Drowjam  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 34
  • Joined: 16-October 12

Using icomparable to create a sort method.

Posted 08 November 2012 - 10:39 PM

Ok I'm kinda new to all so I was investigating some of microsofts class and functions
I came across array.sort which use Icomparable to sort arrays. I was thinking it would be nice at the very least to learn how it works

So I created a custom class and implemented icomparable it was really easy it even gave me a nice little method to work inside.

Sry no code block not sure how to do from phone.

This was the class I kept it simple
Public class TestIC
Implements icomparable 
Public val1 as integer
Public val2 as integer

Public function compareTo(obj as object) as integer implements icomparable.compareTo
Dim g as test = ctype( obj,test)
If g.val1 + g.val2 > me.val1 + me.val2 then
Return 1
Else if g.val1 + g.val2 < me.val1 + me.val2 then
Return -1
Else
Return 0
End if
End function 
End class


This basically would be the same as sorting points from the top left to the bottom right.
I have evolved it a little bit to sort them based on there degree in a circle but for simplicity sake I went with a simpler explanation

Next I wanted to write a bubble sort to handle the actual sorting myself and since a bubble sorts pretty easy to make It seemed a good choice
I then looked to microsofts array.sort method which specified an array as an array for input

So I wrote a mimic sub
Public sub bub_sort(byval ary() as array)
End sub


This is where I had trouble u see when I access ary(0) it dosent have the compareTo method I need to recieve the 1,0,-1 values I need to compare the items in the array
So I thought I need to find the objects type first but that proved futile
So my question is this giving the data in the first overload of the array.sort method ary as array how do I gain access to the icomparable compareTo method??? Thanks

This post has been edited by AdamSpeight2008: 09 November 2012 - 05:12 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Using icomparable to create a sort method.

#2 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 233
  • View blog
  • Posts: 770
  • Joined: 19-October 11

Re: Using icomparable to create a sort method.

Posted 09 November 2012 - 02:11 AM

If your bub_sort method is inside TestIC class, then this should work:
        For n = 0 To ary.Count - 1
            If n <> ary.Count - 1 Then
                Dim something = n.CompareTo(n + 1)
            End If
        Next



If you want to use CompareTo function of TestIC class from outside, then you need live instance of the class. You can't have Public Shared Function CompareTo(obj As Object) As Integer Implements System.IComparable.CompareTo and pass it two values to compare without live instance of TextIC class, because that would change the signature of IComparable.CompareTo function.
Was This Post Helpful? 0
  • +
  • -

#3 Drowjam  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 34
  • Joined: 16-October 12

Re: Using icomparable to create a sort method.

Posted 09 November 2012 - 05:03 PM

View Postlucky3, on 09 November 2012 - 02:11 AM, said:

If your bub_sort method is inside TestIC class, then this should work:
        For n = 0 To ary.Count - 1
            If n <> ary.Count - 1 Then
                Dim something = n.CompareTo(n + 1)
            End If
        Next



If you want to use CompareTo function of TestIC class from outside, then you need live instance of the class. You can't have Public Shared Function CompareTo(obj As Object) As Integer Implements System.IComparable.CompareTo and pass it two values to compare without live instance of TextIC class, because that would change the signature of IComparable.CompareTo function.

Yes you get what I'm wanting I need to take the data in my array and get it's class then call the method
I tried this but it still did not work
Ary(0) = ctype dynamic(ar(0),ary(0).gettype)
Is there a way I can get the array to change into a type that can be compared? I know it can be done Microsoft done I just wanna know how they did it
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Using icomparable to create a sort method.

Posted 09 November 2012 - 06:27 PM

I prefer to implement IComparable(Of T) rather than IComparable this insures the args are of the correct type, which you ain't checking.

Function CompareTo(other As Object) As Integer Implements IComparable.Compare
 ' Checks against type I can not be compared to '
 If other Is Nothing Then Throw New Exception
 If TypeOf other IsNot IComparable Then Throw New Exception
 If TypeOf other IsNot GetType(TestIC) Then Throw New Exception
 Dim c= TryCast(other, TestIC)
 If c Is Nothing Then throw New Exception
 ' implement the actual comparison 
End Function


The Object is mother base class of all .net objects means that any type of object can pass the method.

Public Sub Bubble(Of T As IComparable(Of T))(ByRef ary As T() )
' This method will side effect the array passed in. '



The (Of T As IComparable(Of T) is the generic argument (restraint to only types the implement IComparable(OF T))

I wrote a tutorial on the subject of implement custom comparators and sorting.

This post has been edited by AdamSpeight2008: 09 November 2012 - 06:39 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1