6 Replies - 3406 Views - Last Post: 08 October 2009 - 11:49 AM Rate Topic: -----

#1 Ameel  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 171
  • Joined: 19-June 08

Sorting items in a custom class according to item values

Posted 08 October 2009 - 10:53 AM

Hi. I have the following in my main form

Class MainWindow
Public TableDetails As New List(Of TableDetailsContainer(Of TableXDetails))

	Public Sub ArrangeByID()
'''STUCK HERE''''
	End Sub

	Public Sub ArrangeManuallyByID(ByVal index As Integer)
''''STUCK HERE''''
		Dim temp As New TableXDetails(TableDetails(index).column(0).Name, TableDetails(index).column(0).Type, TableDetails(index).column(0).Value, TableDetails(index).column(0).ID)
	End Sub

	Public Sub PopulateTest()
		Dim temp As New TableDetailsContainer(Of TableXDetails)("Table 1")

		For i As Integer = 30 To 25 Step -1
			temp.add(New TableXDetails("name", "string", "value1", i))
		Next

		For i As Integer = 15 To 10 Step -1
			temp.add(New TableXDetails("name", "string", "value1", i))
		Next

		For i As Integer = 5 To 8
			temp.add(New TableXDetails("name", "string", "value1", i))
		Next

		For i As Integer = 18 To 23
			temp.add(New TableXDetails("name", "string", "value1", i))
		Next

		TableDetails.Add(temp)

	End Sub

End Class


I have the following 2 classes as well:
Public Class TableDetailsContainer(Of TableXDetails)
	Private myList As New List(Of TableXDetails)
	Private myName As String

	Public Sub sort()
''''I Doubt this is any good''''
		myList.Sort()
	End Sub

	Sub New(ByVal Name1 As String)
		Name = Name1
	End Sub
	Public Property Name() As String

		Get

			Return myName

		End Get

		Set(ByVal value As String)

			myName = value

		End Set

	End Property

	Public Function column(ByVal pos As Integer) As TableXDetails
		Return myList(pos)
	End Function

	Public Sub add(ByVal c As TableXDetails)
		Name = Name
		myList.Add(c)
	End Sub

	Public Sub clear()
		myList.Clear()
	End Sub

	Public Sub removeat(ByVal i As Integer)
		myList.RemoveAt(i)
	End Sub


	Public ReadOnly Property count() As Integer
		Get
			Return myList.Count
		End Get
	End Property

End Class

Public Class TableXDetails

	Private mName As String
	Private mValue As String
	Private mType As String
	Private mID As String

	Sub New(ByVal name1 As String, ByVal type1 As String, ByVal value1 As String, ByVal ID1 As String)
		Type = type1
		Name = name1
		Value = value1
		ID = ID1

	End Sub

	Public Property ID() As String

		Get

			Return mID

		End Get

		Set(ByVal value As String)

			mID = value

		End Set

	End Property

	Public Property Type() As String

		Get

			Return mType

		End Get

		Set(ByVal value As String)

			mType = value

		End Set

	End Property

	Public Property Name() As String

		Get

			Return mName

		End Get

		Set(ByVal value As String)

			mName = value

		End Set

	End Property

	Public Property Value() As String

		Get

			Return mValue

		End Get

		Set(ByVal value As String)

			mValue = value

		End Set

	End Property

End Class


What I basically want to achieve is to sort TableDetails(i) items by their "ID", but I have absolutely no idea how to go about it. Could anyone please give me pointers?

Also, I actually want to move the columns within TableDetails(i) around, manually. But I'm not too sure how to do that.
Cheers

edit. i researched, and found that I could use "Implements IComparable(Of )" but, i have no idea how to apply it to my classes :/

This post has been edited by Ameel: 08 October 2009 - 10:57 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Sorting items in a custom class according to item values

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

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

Re: Sorting items in a custom class according to item values

Posted 08 October 2009 - 10:55 AM

First is to create a custom sorter, seeCustom Sorting Tutorial
Second is you use LINQ
Was This Post Helpful? 0
  • +
  • -

#3 magius96  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 75
  • View blog
  • Posts: 834
  • Joined: 15-April 09

Re: Sorting items in a custom class according to item values

Posted 08 October 2009 - 11:00 AM

(EDIT)Bah! I was posting to let the OP know to use the IComparable and IComparer interfaces, but after looking at the tutorial you mentioned, I see you already did it...So ya, just ignore this post. Actually a mod can delete this post if they feel like it. :D (/EDIT)

Mod Edit: I don't feel like deleting it

This post has been edited by AdamSpeight2008: 08 October 2009 - 11:05 AM

Was This Post Helpful? 0
  • +
  • -

#4 Ameel  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 171
  • Joined: 19-June 08

Re: Sorting items in a custom class according to item values

Posted 08 October 2009 - 11:24 AM

View PostAdamSpeight2008, on 8 Oct, 2009 - 09:55 AM, said:

First is to create a custom sorter, seeCustom Sorting Tutorial
Second is you use LINQ


Cheers for the tutorial.

WTH is LINQ? bah, ill probably google it :P

View Postmagius96, on 8 Oct, 2009 - 10:00 AM, said:

(EDIT)Bah! I was posting to let the OP know to use the IComparable and IComparer interfaces, but after looking at the tutorial you mentioned, I see you already did it...So ya, just ignore this post. Actually a mod can delete this post if they feel like it. :D (/EDIT)

Mod Edit: I don't feel like deleting it


thx anyways man :)

This post has been edited by Ameel: 08 October 2009 - 11:25 AM

Was This Post Helpful? 0
  • +
  • -

#5 Ameel  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 171
  • Joined: 19-June 08

Re: Sorting items in a custom class according to item values

Posted 08 October 2009 - 11:34 AM

Ahhhh. This is very confusing. I have no idea how to apply the tutorial to my classes. :/
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

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

Re: Sorting items in a custom class according to item values

Posted 08 October 2009 - 11:45 AM

LINQ Tutorial
Was This Post Helpful? 0
  • +
  • -

#7 Ameel  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 171
  • Joined: 19-June 08

Re: Sorting items in a custom class according to item values

Posted 08 October 2009 - 11:49 AM

View PostAdamSpeight2008, on 8 Oct, 2009 - 10:45 AM, said:



cheers Adam. will look into it when i wake up :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1