sort a structure by a string vb.net


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: sort a structure by a string vb.net

  1. #1
    Join Date
    Aug 2006
    Location
    Albuquerque
    Posts
    32

    sort a structure by a string vb.net

    I would like to sort a structure based on a string. In particular I am trying to sort all the items in ItemType. I know how to do it with an integer but was unsure on how to sort a string.


    Code:
    Module Module1
        Dim itemCode(8) As String
        Dim itemType(8) As String
        Dim uHand(8) As Long
        Dim aUnit(8) As Single
        Dim sUnit(8) As Single
        Dim i As Integer
        Dim fs As New FileStream("file.txt", FileMode.Create, FileAccess.Write)
        Dim s As New StreamWriter(fs)
        Dim hdr As String
        Dim bdr As String
        Dim arr As String
        Dim tCost As Long
        Dim tSales As Long
        Dim tIncome As Long
        Dim tUhand As Long
        Dim str As String 
    
     Structure dFile
            Public ItemCode As String
            Public ItemType As String
            Public uhand As Long
            Public aunit As Single
            Public sunit As Single
       
        End Structure
      
    Sub Main()
            arrLoad()
            crFile()
            rOne()
    
            End Sub
    
    
     Sub arrLoad()
    
            IC() 'load arrays
            IT()
            uH()
            aU()
            sU()
    
            Dim dataFile(8) As dFile
            For I As Integer = 0 To 8
                dataFile(I).ItemCode = itemCode(I)
                dataFile(I).ItemType = itemType(I)
                dataFile(I).uhand = uHand(I)
                dataFile(I).aunit = aUnit(I)
                dataFile(I).sunit = sUnit(I)
    
            Next
    
        End Sub
    
    
     Sub crFile()
                For i = 0 To 8
                    s.WriteLine(((itemCode(i) & "               " & itemType(i) & "                           " & uHand(i) & _
                "                              " & aUnit(i) & "                                " & sUnit(i))))
                    tCost += (uHand(i) * aUnit(i))
                    tSales += (uHand(i) * sUnit(i))
                Next
                tIncome += (tSales - tCost)
                s.Close()
            End Sub

  2. #2
    Join Date
    Nov 2003
    Posts
    154
    The simplest way is to change your structure to implement IComparable. For example:

    Structure dFile
    Implements IComparable
    Public ItemCode As String
    Public ItemType As String
    Public uhand As Long
    Public aunit As Single
    Public sunit As Single
    Public Function CompareTo(ByVal obj As Object) As Integer _
    Implements IComparable.CompareTo
    Dim df As dFile = CType(obj, dFile)
    Return Me.ItemType.CompareTo(df.ItemType)
    End Function
    End Structure

    Having done that, you can use the built-in Array.Sort method on your array of dFile structures.
    A. Russell Jones,
    Executive Editor,
    Internet.com

  3. #3
    Join Date
    Aug 2006
    Location
    Albuquerque
    Posts
    32
    Thanks for the help! I was able to sort by item type, but did not keep the structure together when doing so. I had records that were being mismatched. Where did I go wrong?


    Code:
      Structure dFile
            Implements IComparable
            Public ItemCode As String
            Public ItemType As String
            Public uhand As Long
            Public aunit As Single
            Public sunit As Single
            Public Function CompareTo(ByVal obj As Object) As Integer _
            Implements IComparable.CompareTo
                Dim datafile As dFile = CType(obj, dFile)
                Return Me.ItemType.CompareTo(datafile.ItemType)
            End Function
        End Structure
        Sub arrLoad()
    
            IC() 'load arrays
            IT()
            uH()
            aU()
            sU()
    
            Dim datafile(8) As dFile
            For I As Integer = 0 To 8
                dataFile(I).ItemCode = itemCode(I)
                dataFile(I).ItemType = itemType(I)
                dataFile(I).uhand = uHand(I)
                dataFile(I).aunit = aUnit(I)
                dataFile(I).sunit = sUnit(I)
    
            Next
            Array.Sort(datafile, itemType)
    End Sub

Similar Threads

  1. Writing in HKEY_LOCAL_MACHINE...Access is denied
    By Martin in forum VB Classic
    Replies: 22
    Last Post: 12-03-2001, 04:53 AM
  2. Replies: 1
    Last Post: 06-05-2001, 07:12 AM
  3. Technical issues with VB.NET
    By Patrick Ireland in forum .NET
    Replies: 11
    Last Post: 01-30-2001, 02:02 PM
  4. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 0
    Last Post: 08-10-2000, 10:16 PM
  5. NetUserEnum Api
    By Mike in forum VB Classic
    Replies: 2
    Last Post: 04-24-2000, 04:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center