Results 1 to 11 of 11

Thread: [RESOLVED] Sorting two arrays simultaneously

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    24

    Resolved [RESOLVED] Sorting two arrays simultaneously

    Okay, so I'm trying to sort two arrays simultaneously. One that keeps track of the score, and one for the name.

    I have the complete code to sort the score by descending size. MyArray is for score, and MyArray2 is for the names.

    That means that the one who got the score MyArray(0) has the name; MyArray2(0), and so on.

    How can I after sorting, get the names to follow the score? If you know what I mean..

    Code:
    Dim iOuter As Long
    Dim iInner As Long
    Dim iLBound As Long
    Dim iUBound As Long
    Dim iTemp As Long
    Dim iMax As Long
    Dim MyArray() As String
    Dim MyString As String
    Dim MyArray2() As String
    Dim MyString2 As String
    Dim FF2 As Integer
    Dim FF As Integer
    
    Private Sub Command1_Click()
        For i = LBound(MyArray) To UBound(MyArray)
    List1.AddItem (MyArray(i))
    Next
    End Sub
    
    Private Sub Form_load()
    FF = FreeFile()
    Open ("C:\Documents and Settings\Administratör.MATRIX\Mina dokument\Score.txt") For Binary As FF
    
    MyString = Space(LOF(FF))
    Get FF, , MyString
    Close FF
    
    FF2 = FreeFile()
    Open ("C:\Documents and Settings\Administratör.MATRIX\Mina dokument\Name.txt") For Binary As FF2
    
    
    MyString2 = Space(LOF(FF2))
    Get FF2, , MyString2
    Close FF2
    
    MyString = Left(MyString, Len(MyString) - 1)
    MyString2 = Left(MyString2, Len(MyString2) - 1)
    
    MyArray = Split(MyString, ",")
    MyArray2 = Split(MyString2, ",")
    
        iLBound = LBound(MyArray)
        iUBound = UBound(MyArray)
        
        For iOuter = iUBound To iLBound + 1 Step -1
            
            iMax = 0
            
            For iInner = iLBound To iOuter
                If CLng(MyArray(iInner)) < CLng(MyArray(iMax)) Then iMax = iInner
            Next iInner
            
            iTemp = MyArray(iMax)
            MyArray(iMax) = MyArray(iOuter)
            MyArray(iOuter) = iTemp
            
        Next iOuter
        
            For i = LBound(MyArray) To UBound(MyArray)
            MyArray(i) = Format(MyArray(i), "#,##")
    List1.AddItem (MyArray(i))
    Next
    End Sub

  2. #2
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,526

    Re: Sorting two arrays simultaneously

    You need to move the MyArray2 elements when you move the MyArray elements.

    Code:
    Dim iOuter As Long
    Dim iInner As Long
    Dim iLBound As Long
    Dim iUBound As Long
    Dim iTemp As Long
    Dim iMax As Long
    Dim MyArray() As String
    Dim MyString As String
    Dim MyArray2() As String
    Dim MyString2 As String
    Dim FF2 As Integer
    Dim FF As Integer
    
    Private Sub Command1_Click()
        For i = LBound(MyArray) To UBound(MyArray)
    List1.AddItem (MyArray(i))
    Next
    End Sub
    
    Private Sub Form_load()
    FF = FreeFile()
    Open ("C:\Documents and Settings\Administratör.MATRIX\Mina dokument\Score.txt") For Binary As FF
    
    MyString = Space(LOF(FF))
    Get FF, , MyString
    Close FF
    
    FF2 = FreeFile()
    Open ("C:\Documents and Settings\Administratör.MATRIX\Mina dokument\Name.txt") For Binary As FF2
    
    
    MyString2 = Space(LOF(FF2))
    Get FF2, , MyString2
    Close FF2
    
    MyString = Left(MyString, Len(MyString) - 1)
    MyString2 = Left(MyString2, Len(MyString2) - 1)
    
    MyArray = Split(MyString, ",")
    MyArray2 = Split(MyString2, ",")
    
        iLBound = LBound(MyArray)
        iUBound = UBound(MyArray)
        
        For iOuter = iUBound To iLBound + 1 Step -1
            
            iMax = 0
            
            For iInner = iLBound To iOuter
                If CLng(MyArray(iInner)) < CLng(MyArray(iMax)) Then iMax = iInner
            Next iInner
            
            iTemp = MyArray(iMax)
            MyArray(iMax) = MyArray(iOuter)
            MyArray(iOuter) = iTemp
    
            iTemp = MyArray2(iMax)
            MyArray2(iMax) = MyArray2(iOuter)
            MyArray2(iOuter) = iTemp
    
            
        Next iOuter
        
            For i = LBound(MyArray) To UBound(MyArray)
            MyArray(i) = Format(MyArray(i), "#,##")
    List1.AddItem (MyArray(i))
    Next
    End Sub

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    24

    Re: Sorting two arrays simultaneously

    I'm not quite following you.
    Do you mean that I have to make a copy of the number sorting, and make it MyArray2?
    Because I saw you added this code:
    Code:
     iTemp = MyArray2(iMax)
            MyArray2(iMax) = MyArray2(iOuter)
            MyArray2(iOuter) = iTemp

  4. #4
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,526

    Re: Sorting two arrays simultaneously

    As long as both arrays have the same number of elements, and MyArray(x) corresponds to MyArray2(x), then once you have determined where to move the MyArray(x) element you just move the MyArray2(x) element to the same location. Did you actually try the updated code?

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    24

    Re: Sorting two arrays simultaneously

    I did try the updated code and it gave me error: 13 I believe .

  6. #6
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Sorting two arrays simultaneously

    The decision to swap is based on the value of MyArray and MyArray2 is merely carried along with it.
    Doctor Ed

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Sorting two arrays simultaneously

    I don't have time to put together an example but the easiest thing to do would be to create a UDT array containing Name and Score and then just sort that one array.

  8. #8
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Sorting two arrays simultaneously

    Quote Originally Posted by MartinLiss
    I don't have time to put together an example but the easiest thing to do would be to create a UDT array containing Name and Score and then just sort that one array.
    Excellent idea, Martin. I believe the score component of the UDT would simply have to appear first in the Type sequence.

    I've usually only used separate variable arrays in my Apps because options are offered to sort the values of any one of them shown in a grid.
    Doctor Ed

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Sorting two arrays simultaneously

    Quote Originally Posted by Code Doc
    Excellent idea, Martin. I believe the score component of the UDT would simply have to appear first in the Type sequence.

    I've usually only used separate variable arrays in my Apps because options are offered to sort the values of any one of them shown in a grid.
    No, it can be in either place. You just need to compare the score component.

  10. #10
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Sorting two arrays simultaneously

    Here's an example.

    Code:
    Option Explicit
    Private Type HiScores
        PlayerName As String
        Score As Integer
    End Type
    Private Sub Form_Load()
        Dim HS(4) As HiScores
        Dim TempHS As HiScores
        Dim IsSwapped As Boolean
        Dim intIndex As Integer
    
        HS(0).PlayerName = "Marty"
        HS(0).Score = 100
        
        HS(1).PlayerName = "Fred"
        HS(1).Score = 90
        
        HS(2).PlayerName = "Sam"
        HS(2).Score = 33
        
        HS(3).PlayerName = "Joe"
        HS(3).Score = 66
        
        IsSwapped = True
        Do Until IsSwapped = False
            IsSwapped = False
            For intIndex = 1 To UBound(HS)
                If HS(intIndex).Score >= HS(intIndex - 1).Score Then
                    TempHS.PlayerName = HS(intIndex).PlayerName
                    TempHS.Score = HS(intIndex).Score
                    HS(intIndex).PlayerName = HS(intIndex - 1).PlayerName
                    HS(intIndex).Score = HS(intIndex - 1).Score
                    HS(intIndex - 1).PlayerName = TempHS.PlayerName
                    HS(intIndex - 1).Score = TempHS.Score
                    IsSwapped = True
                    Exit For
                End If
            Next
        Loop
    
        For intIndex = 0 To UBound(HS)
            Debug.Print HS(intIndex).PlayerName; HS(intIndex).Score
        Next
    
    End Sub

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    24

    Re: Sorting two arrays simultaneously

    Thanks everyone for the kind answers.

    I publish the solution for anyone to use
    Code:
    im iOuter As Long
    Dim iInner As Long
    Dim iLBound As Long
    Dim iUBound As Long
    Dim iTemp As Long
    Dim iMax As Long
    Dim MyArray() As String
    Dim MyString As String
    Dim MyArray2() As String
    Dim MyString2 As String
    Dim FF2 As Integer
    Dim FF As Integer
    
    Private Sub Command1_Click()
    Form1.Show
    
    Form2.Hide
    End Sub
    
    Private Sub Form_load()
    FF = FreeFile()
    Open ("C:\Score.txt") For Binary As FF
    
    MyString = Space(LOF(FF))
    Get FF, , MyString
    Close FF
    
    FF2 = FreeFile()
    Open ("C:\Name.txt") For Binary As FF2
    
    MyString2 = Space(LOF(FF2))
    Get FF2, , MyString2
    Close FF2
    
    MyString = Left(MyString, Len(MyString) - 1)
    MyString2 = Left(MyString2, Len(MyString2) - 1)
    
    MyArray = Split(MyString, ",")
    MyArray2 = Split(MyString2, ",")
    
        iLBound = LBound(MyArray)
        iUBound = UBound(MyArray)
        
        For iOuter = iUBound To iLBound + 1 Step -1
            
            iMax = 0
            
            For iInner = iLBound To iOuter
                If CLng(MyArray(iInner)) < CLng(MyArray(iMax)) Then iMax = iInner
            Next iInner
            
            iTemp = MyArray(iMax)
            ntemp$ = MyArray2(iMax)
            
            MyArray(iMax) = MyArray(iOuter)
            MyArray2(iMax) = MyArray2(iOuter)
            
            MyArray(iOuter) = iTemp
            MyArray2(iOuter) = ntemp$
        Next iOuter
    
            For i = LBound(MyArray) To UBound(MyArray)
            MyArray(i) = Format(MyArray(i), "#,##")
    
            List1.AddItem (MyArray(i) + "  " + MyArray2(i))
        Next
         
    End Sub
    Last edited by MrDudemeister; Nov 20th, 2008 at 11:46 AM.

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