|
-
Jun 16th, 2004, 02:56 AM
#5
The performance for the above, to my knowledge, gets cumulatively worse for each added item. Here is a solution which requires less calculating with big arrays (but uses more memory too):
VB Code:
Option Explicit
Private Type PlayerArray
PlayerID As Long
Firstname As String
Lastname As String
Weight As Byte
End Type
Dim aPlayer() As PlayerArray
Private Sub SortArray()
Dim TempArray As New Collection, A As Long, B As Long, C As Long
Dim SortedArray() As PlayerArray
TempArray.Add 0
For A = 1 To UBound(aPlayer)
For B = 1 To TempArray.Count
C = CLng(TempArray(B))
If aPlayer(A).PlayerID < aPlayer(C).PlayerID Then TempArray.Add A, , B: Exit For
Next B
'check if we exited from the loop or not
If B > TempArray.Count Then TempArray.Add A
Next A
ReDim SortedArray(UBound(aPlayer))
For A = 1 To TempArray.Count
B = CLng(TempArray(A))
With SortedArray(A - 1)
.PlayerID = aPlayer(B).PlayerID
.Firstname = aPlayer(B).Firstname
.Lastname = aPlayer(B).Lastname
.Weight = aPlayer(B).Weight
End With
Next A
For A = 0 To UBound(aPlayer)
aPlayer(A).PlayerID = SortedArray(A).PlayerID
aPlayer(A).Firstname = SortedArray(A).Firstname
aPlayer(A).Lastname = SortedArray(A).Lastname
aPlayer(A).Weight = SortedArray(A).Weight
Next A
End Sub
Private Sub Form_Load()
Dim A As Byte
ReDim aPlayer(2)
With aPlayer(0)
.PlayerID = 1
.Firstname = "Jack"
.Lastname = "Humblerose"
.Weight = 80
End With
With aPlayer(1)
.PlayerID = 1
.Firstname = "Jack"
.Lastname = "Abrahamson"
.Weight = 90
End With
With aPlayer(2)
.PlayerID = 0
.Firstname = "Jack"
.Lastname = "Rogers"
.Weight = 100
End With
SortArray
For A = 0 To 2
With aPlayer(A)
Debug.Print "PlayerID: " & .PlayerID
Debug.Print "Firstname: " & .Firstname
Debug.Print "Lastname: " & .Lastname
Debug.Print "Weight: " & .Weight
End With
Next A
End Sub
You can get it faster if you can figure out how to use CopyMemory to copy complete data from SortedArray to aPlayer... I tried playing with it, but since I couldn't figure it out in my limited time (have to go to work soon), I didn't do it.
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
|