Results 1 to 9 of 9

Thread: Sorting Collections

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Location
    Trenton OH (near Cincinnati)
    Posts
    4

    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.

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  3. #3
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Location
    Trenton OH (near Cincinnati)
    Posts
    4
    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.

  5. #5
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Location
    Trenton OH (near Cincinnati)
    Posts
    4
    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(filenameFileMode.Open)
        
    ''  load the object from the stream...
        
    Dim newObject As Object Load(streamnewType)
        
    ''  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.ObjectByVal e As System.EventArgsHandles MyBase.Load
        
    ''  load the Control Table...
        
    Table SerializableData.Load(DataFileNameGetType(ControlTable))
        
    Table.Items.Sort()  <<<<<<<<<<<<<<<<<<<<<<<<<<<<
      
    End Sub 

  7. #7
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Location
    Trenton OH (near Cincinnati)
    Posts
    4
    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.

  9. #9
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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:
    1. Private Sub SortIt()
    2.        ' Here I get the folder info
    3.        Dim dr As New DirectoryInfo("D:\test")
    4.        ' if the folder exists then get the txt files in an array
    5.        If dr.Exists Then
    6.           Dim ff() As FileInfo
    7.           ff = dr.GetFiles("*.txt")
    8.           ' if any file exist then sort them according to their property i set in compclass
    9.           If ff.Length > 0 Then
    10.              Array.Sort(ff, New compclass(SortOrder.Descending))
    11.            ' some code here to deal with the sorted array
    12.           End If
    13.        End If                  
    14. End Sub
    15.  
    16. Public Class compclass
    17.         Implements IComparer
    18.         Private srt As SortOrder
    19.         Public Sub New(ByVal srtorder As SortOrder)
    20.             srt = srtorder
    21.         End Sub
    22.         Public Function compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
    23.             If srt = SortOrder.Descending Then
    24.             Return (DateTime.Compare(CType(x, FileInfo).CreationTime, CType(y, FileInfo).CreationTime) * (-1))
    25.             Else
    26.                 Return DateTime.Compare(CType(x, FileInfo).CreationTime, CType(y, FileInfo).CreationTime)
    27.             End If
    28.              ' Here you can use your objects, and the code to compare will be like
    29.             'Return Typeyouwanttocompare.Compare(CType(x, myObject).myProperty, CType(y, myObject).myProperty)
    30.  
    31.         End Function
    32. 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
  •  



Click Here to Expand Forum to Full Width