Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Im creating a general sorting algorythm, pretty simple but i want it to be able to sort any array,....

so lets say i have an array of blahs

public structure blah

public x,y as integer

end structure

and i want this array sorted by the x's...

public sub sort(array() as object,subobject as ??????)

if array(i).subobject < array(i+1).subobject then

.......

end if

....

end sub

sorry for the shortened code but the concept is so simple. i just need to know how to pass elemnts to the functions / subs.. Thanks for all your help

  • Administrators
Posted

Is there any reason why you don't want to use the build in Array.Sort? You would just need to implement the IComparable interface.

 

Public Structure blah
	Implements IComparable

	Public x, y As Integer

	Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
		If Not TypeOf obj Is blah Then
			Throw New InvalidOperationException("Can only compare to Blah things")
		End If

		Dim b As blah = DirectCast(obj, blah)
		If Me.x > b.x Then Return 1
		If Me.x < b.x Then Return -1
		Return 0

	End Function
End Structure

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
	Dim arr(4) As blah
	'populate arr()
	Dim r As New Random
	arr(0).x = r.Next
	arr(1).x = r.Next
	arr(2).x = r.Next
	arr(3).x = r.Next
	arr(4).x = r.Next

	Array.Sort(arr)
End Sub

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

The only thing is

 

That way means i have to program compairto's into every structure, and if i want to sort by x's first then latter sort by y's or sort by x's and then sort by y's the code gets more and more complicated, if i could find a way to send a function which element it is it would be much easyer,.. and would be usefull in other instances which are a bit more complicated

Posted (edited)

Maybe you could create an abstract base class that implements IComparable, and have all other classes inherit from the base? It's possible to build enough logic into the IComparable implementation to cover quite a few different scenarios.

 

For example, in the past I've had to sort giant lists of various business objects. Some objects had letters as names, while others used numbers (IP Addresses had to be sorted and obviously couldn't be handled the same way as sorting contacts). It was relatively easy to build the logic into my algorithm to handle several different sorting needs.

Edited by Rodenberg

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...