|
-
Apr 29th, 2003, 09:02 AM
#1
Thread Starter
New Member
Sorting Collections
I have created a collection of text items ("2", "1", "This is the first item in the second area") and I would like to sort the collection before I write it back to an XML file. (I am new to VB.NET and am having trouble understanding the syntax of the SORT method in the HELP system of VS.NET ) Thanks.
-
Apr 29th, 2003, 10:13 AM
#2
There is also a SortedList class that is derived from a collection.
Hey what happened to Lunatic3's post that was before mine?
Last edited by Edneeis; Apr 29th, 2003 at 10:28 AM.
-
Apr 29th, 2003, 10:17 AM
#3
Frenzied Member
Sorry I had to change the response to make it more clear.
When you want to sort a collection if the collection Impelements Icomparable Interface then you can use Array.Sort method. If the objects does not support Icompareable Interface then you may build a custom IComparer class to be used by sort method of array.
As string implement Icomparable interface you can sort them using: Array.Sort(myarray)
Last edited by Lunatic3; Apr 29th, 2003 at 10:21 AM.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
May 6th, 2003, 04:21 PM
#4
Thread Starter
New Member
I am making progress after some interruptions.
The follow line gives me a "Specified IComparer threw an exception"
Table.Items.Sort()
What is the axact line of code that I need to add. I do not understand want I need to add. Thanks.
-
May 6th, 2003, 04:45 PM
#5
Frenzied Member
Post some more codes to check.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
May 6th, 2003, 09:23 PM
#6
Thread Starter
New Member
Here are the parts of the code you might need. The offending line of code is in the last section marked with "<<<<<<". I hope this is what you need. Thanks again.
*******************************************
PHP Code:
Imports System.IO
Imports System.Xml.Serialization
Public Class SerializableData
'' load - deserialize from disk...
Public Shared Function Load(ByVal filename As String, _
ByVal newType As Type) As Object
'' does file exist...
Dim fileinfo As New FileInfo(filename)
If fileinfo.Exists = False Then
'' create a blank version of the object and return that...
Return System.Activator.CreateInstance(newType)
End If
'' open the file...
Dim stream As New FileStream(filename, FileMode.Open)
'' load the object from the stream...
Dim newObject As Object = Load(stream, newType)
'' close stream
stream.Close()
'' return the object...
Return newObject
End Function
'' shared Load function...
Public Shared Function Load(ByVal stream As Stream, _
ByVal newType As Type) As Object
'' create a serializer and load the object...
Dim serializer As New XmlSerializer(newType)
'' Dim newObject As Object = serializer.Deserialize(stream)
Dim newObject As Object = serializer.Deserialize(stream)
'' return the new object...
Return newObject
End Function
End Class
********************************************
PHP Code:
Imports System.Xml.Serialization
Public Class ControlTable
Inherits SerializableData
'' members of the class...
<XmlIgnore()> Public Items As New ArrayList()
'' Records property that works with the
'' items collection as an array...
Public Property Records() As ControlRecord()
Get
'' create a new array...
Dim recordArray(Items.Count - 1) As ControlRecord
Items.CopyTo(recordArray)
Return recordArray
End Get
Set(ByVal Value As ControlRecord())
'' reset the arrayList...
Items.Clear()
'' did we get anything...
If Not Value Is Nothing Then
'' go through the array and populate items...
Dim ControlRecord As ControlRecord
For Each ControlRecord In Value
Items.Add(ControlRecord)
Next
End If
End Set
End Property
End Class
********************************************
PHP Code:
Private Sub frmCtrlRecMaintenance_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'' load the Control Table...
Table = SerializableData.Load(DataFileName, GetType(ControlTable))
Table.Items.Sort() <<<<<<<<<<<<<<<<<<<<<<<<<<<<
End Sub
-
May 7th, 2003, 12:15 PM
#7
Frenzied Member
Sorry, I got nowhere with the code, please describe what you trying to do, what is 'ContorlRecord'?
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
May 9th, 2003, 12:16 PM
#8
Thread Starter
New Member
In the third section of the code, I create a new instance of the collections file that was read from the disk via FileStream in System.XML.Serialation called Table. That Table collection object is what I want to sort. I think that I have to implement SortedList class but how do you do that. Thanks for spending the time on this. The only two imports that I have are System.IO and System.XML.Serialation.
-
May 9th, 2003, 01:35 PM
#9
Frenzied Member
This is how I sort the files of a folder according to their creation time. I am not sure if this code is perfect but it does its job for me, any idea for improvement of the code will be greatly appreciated.
I think you can modify it according to your needs:
VB Code:
Private Sub SortIt()
' Here I get the folder info
Dim dr As New DirectoryInfo("D:\test")
' if the folder exists then get the txt files in an array
If dr.Exists Then
Dim ff() As FileInfo
ff = dr.GetFiles("*.txt")
' if any file exist then sort them according to their property i set in compclass
If ff.Length > 0 Then
Array.Sort(ff, New compclass(SortOrder.Descending))
' some code here to deal with the sorted array
End If
End If
End Sub
Public Class compclass
Implements IComparer
Private srt As SortOrder
Public Sub New(ByVal srtorder As SortOrder)
srt = srtorder
End Sub
Public Function compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
If srt = SortOrder.Descending Then
Return (DateTime.Compare(CType(x, FileInfo).CreationTime, CType(y, FileInfo).CreationTime) * (-1))
Else
Return DateTime.Compare(CType(x, FileInfo).CreationTime, CType(y, FileInfo).CreationTime)
End If
' Here you can use your objects, and the code to compare will be like
'Return Typeyouwanttocompare.Compare(CType(x, myObject).myProperty, CType(y, myObject).myProperty)
End Function
End Class
I have no idea about SortedList yet.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|