dood
01-14-2008, 06:33 PM
I am to program code that sorts multiple names in arrays into alphabetical order. I have the code written and I have checked over it many times, but it is not working.
Function ascend(inName As String, position As Integer) As Integer
ascend = Asc(Mid(inName, position, 1))
End Function
This just gets the character from a string in the position stated in the parameter.
Private Sub cmdImport_Click()
filename = InputBox("What is the name of the file you wish to import?") 'ask for filename
filename = "/" + filename 'add / to filename
Open App.Path + filename For Input As #1 'open file to use
Do While EOF(1) = False
i = i + 1 'add 1 to counter
Input #1, nam(i), phNum(i) 'get name and number
lblName(i).Caption = nam(i) 'display name, number and entry number
lblNumber(i).Caption = phNum(i)
lblEntry(i).Caption = i
Loop
Close
MsgBox "File has been imported. Now able to save and add contacts.", , "Alert"
End Sub
This imports the contacts and adds to a counter (i) each time.
Private Sub cmdSortA_Click()
Dim k As Integer
Dim q As Integer
Dim comp1, comp2 As Integer
Dim tempName, tempNum As String
If filename = "" Then
MsgBox "Please import a file before saving.", vbCritical, "ERROR" 'if user did not select a file
Exit Sub
End If
For x = lblEntry.LBound To i
For j = lblEntry.LBound To i - 1
k = 1
comp1 = ascend(lblName(j).Caption, k)
comp2 = ascend(lblName(j + 1).Caption, k)
If comp1 = comp2 Then
Do Until comp1 <> comp2
k = k + 1
comp1 = ascend(lblName(j).Caption, k)
comp2 = ascend(lblName(j + 1).Caption, k)
Loop
End If
If comp2 < comp1 Then
tempName = nam(j)
tempNum = phNum(j)
nam(j) = nam(j + 1)
phNum(j) = phNum(j + 1)
nam(j + 1) = tempName
phNum(j + 1) = tempNum
End If
Next j
Next x
For q = 1 To i
lblName(q).Caption = nam(q)
lblNumber(q).Caption = phNum(q)
Next
End Sub
k is reset each time the loop is started, so when the loop runs fresh only the first character changes. If the characters are the same, it executes a loop that checks each character until they aren't. It checks the next character by adding 1 to k (position of character). Lastly, it checks if the second is smaller than the first and switches the two if they are.
Function ascend(inName As String, position As Integer) As Integer
ascend = Asc(Mid(inName, position, 1))
End Function
This just gets the character from a string in the position stated in the parameter.
Private Sub cmdImport_Click()
filename = InputBox("What is the name of the file you wish to import?") 'ask for filename
filename = "/" + filename 'add / to filename
Open App.Path + filename For Input As #1 'open file to use
Do While EOF(1) = False
i = i + 1 'add 1 to counter
Input #1, nam(i), phNum(i) 'get name and number
lblName(i).Caption = nam(i) 'display name, number and entry number
lblNumber(i).Caption = phNum(i)
lblEntry(i).Caption = i
Loop
Close
MsgBox "File has been imported. Now able to save and add contacts.", , "Alert"
End Sub
This imports the contacts and adds to a counter (i) each time.
Private Sub cmdSortA_Click()
Dim k As Integer
Dim q As Integer
Dim comp1, comp2 As Integer
Dim tempName, tempNum As String
If filename = "" Then
MsgBox "Please import a file before saving.", vbCritical, "ERROR" 'if user did not select a file
Exit Sub
End If
For x = lblEntry.LBound To i
For j = lblEntry.LBound To i - 1
k = 1
comp1 = ascend(lblName(j).Caption, k)
comp2 = ascend(lblName(j + 1).Caption, k)
If comp1 = comp2 Then
Do Until comp1 <> comp2
k = k + 1
comp1 = ascend(lblName(j).Caption, k)
comp2 = ascend(lblName(j + 1).Caption, k)
Loop
End If
If comp2 < comp1 Then
tempName = nam(j)
tempNum = phNum(j)
nam(j) = nam(j + 1)
phNum(j) = phNum(j + 1)
nam(j + 1) = tempName
phNum(j + 1) = tempNum
End If
Next j
Next x
For q = 1 To i
lblName(q).Caption = nam(q)
lblNumber(q).Caption = phNum(q)
Next
End Sub
k is reset each time the loop is started, so when the loop runs fresh only the first character changes. If the characters are the same, it executes a loop that checks each character until they aren't. It checks the next character by adding 1 to k (position of character). Lastly, it checks if the second is smaller than the first and switches the two if they are.